MyException - 我的异常网
当前位置:我的异常网» C# » 求求高手帮小弟我把下面VC++代码用C#语法写出来好么

求求高手帮小弟我把下面VC++代码用C#语法写出来好么!

www.MyException.Cn  网友分享于:2013-12-18  浏览:21次
求求高手帮我把下面VC++代码用C#语法写出来好么!!
其中RegCreateKeyEx在C#中如何用RegistryKey向注测表写值什么的,请哪们朋友帮完整的用C#语法写一下好么!
谢谢了!小弟是新手,要写的完整些呀!

int InstallDriver(const char* cszDriverName,const char* cszDriverFullPath)
{
char szBuf[LG_PAGE_SIZE];
HKEY hKey;
DWORD dwData;

if( NULL==cszDriverName || NULL==cszDriverFullPath )
{
return -1;
}
memset(szBuf,0,LG_PAGE_SIZE);
strcpy(szBuf,"SYSTEM\\CurrentControlSet\\Services\\");
strcat(szBuf,cszDriverName);
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,szBuf,0,"",0,KEY_ALL_ACCESS,NULL,&hKey,(LPDWORD)&dwData)!=ERROR_SUCCESS)
{
return -1;
}
strcpy(szBuf,cszDriverName);
if(RegSetValueEx(hKey,"DisplayName",0,REG_SZ,(CONST BYTE*)szBuf,(DWORD)strlen(szBuf))!=ERROR_SUCCESS)
{
return -1;
}
dwData=1;
if(RegSetValueEx(hKey,"ErrorControl",0,REG_DWORD,(CONST BYTE*)&dwData,sizeof(DWORD))!=ERROR_SUCCESS)
{
return -1;
}
strcpy(szBuf,"\\??\\");
strcat(szBuf,cszDriverFullPath);
if(RegSetValueEx(hKey,"ImagePath",0,REG_SZ,(CONST BYTE*)szBuf,(DWORD)strlen(szBuf))!=ERROR_SUCCESS)
{
return -1;
}
dwData=3;
if(RegSetValueEx(hKey,"Start",0,REG_DWORD,(CONST BYTE*)&dwData,sizeof(DWORD))!=ERROR_SUCCESS)
{
return -1;
}
dwData=1;
if(RegSetValueEx(hKey,"Type",0,REG_DWORD,(CONST BYTE*)&dwData,sizeof(DWORD))!=ERROR_SUCCESS)
{
return -1;
}
RegFlushKey(hKey);
RegCloseKey(hKey);
strcpy(szBuf,"SYSTEM\\CurrentControlSet\\Services\\");
strcat(szBuf,cszDriverName);
strcat(szBuf,"\\Security");
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,szBuf,0,"",0,KEY_ALL_ACCESS,NULL,&hKey,(LPDWORD)&dwData)!=ERROR_SUCCESS)
{
return -1;
}
dwData=SECURITY_STRING_LEN;
if(RegSetValueEx(hKey,"Security",0,REG_BINARY,g_szSecurity,dwData)!=ERROR_SUCCESS)
{
return -1;
}
RegFlushKey(hKey);
RegCloseKey(hKey);

return 0;
}

------解决方案--------------------
楼主可以自己搜索:“DllImport OpenSCManager SERVICE_QUERY_CONFIG const int ref”这样的关键字,自己找声明的方法。
-_-!!!体力活
参考如下代码:
C# code
public const uint STANDARD_RIGHTS_REQUIRED = 0xF0000;
public const uint SC_MANAGER_CONNECT = 0x1;
public const uint SC_MANAGER_CREATE_SERVICE = 0x2;
public const uint SC_MANAGER_ENUMERATE_SERVICE = 0x4;
public const uint SC_MANAGER_LOCK = 0x8;
public const uint SC_MANAGER_QUERY_LOCK_STATUS = 0x10;
public const uint SC_MANAGER_MODIFY_BOOT_CONFIG = 0x20;
public const uint SC_MANAGER_ALL_ACCESS =
    STANDARD_RIGHTS_REQUIRED + SC_MANAGER_CONNECT +
    SC_MANAGER_CREATE_SERVICE + SC_MANAGER_ENUMERATE_SERVICE +
    SC_MANAGER_LOCK + SC_MANAGER_QUERY_LOCK_STATUS +
    SC_MANAGER_MODIFY_BOOT_CONFIG;

[DllImport("advapi32.dll")]
public static extern IntPtr OpenSCManager(string lpMachineName,
    string lpDatabaseName, uint dwDesiredAccess);

public const uint SERVICE_QUERY_CONFIG = 0x1;
public const uint SERVICE_CHANGE_CONFIG = 0x2;
public const uint SERVICE_QUERY_STATUS = 0x4;
public const uint SERVICE_ENUMERATE_DEPENDENTS = 0x8;
public const uint SERVICE_START = 0x10;
public const uint SERVICE_STOP = 0x20;
public const uint SERVICE_PAUSE_CONTINUE = 0x40;
public const uint SERVICE_INTERROGATE = 0x80;
public const uint SERVICE_USER_DEFINED_CONTROL = 0x100;
public const uint SERVICE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED + SERVICE_QUERY_CONFIG +
    SERVICE_CHANGE_CONFIG + SERVICE_QUERY_STATUS + SERVICE_ENUMERATE_DEPENDENTS +
    SERVICE_START + SERVICE_STOP + SERVICE_PAUSE_CONTINUE + SERVICE_INTERROGATE +
    SERVICE_USER_DEFINED_CONTROL;

[DllImport("advapi32.dll")]
public static extern IntPtr OpenService(
    IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);

[StructLayout(LayoutKind.Sequential)]
public struct SERVICE_STATUS
{
    public int dwServiceType;
    public int dwCurrentState;
    public int dwControlsAccepted;
    public int dwWin32ExitCode;
    public int dwServiceSpecificExitCode;
    public int dwCheckPoint;
    public int dwWaitHint;
}

public const uint SERVICE_CONTROL_STOP = 0x1;
public const uint SERVICE_CONTROL_PAUSE = 0x2;
public const uint SERVICE_CONTROL_CONTINUE = 0x3;
public const uint SERVICE_CONTROL_INTERROGATE = 0x4;
public const uint SERVICE_CONTROL_SHUTDOWN = 0x5;
public const uint SERVICE_CONTROL_PARAMCHANGE = 0x6;
public const uint SERVICE_CONTROL_NETBINDADD = 0x7;
public const uint SERVICE_CONTROL_NETBINDREMOVE = 0x8;
public const uint SERVICE_CONTROL_NETBINDENABLE = 0x9;
public const uint SERVICE_CONTROL_NETBINDDISABLE = 0xA;
public const uint SERVICE_CONTROL_DEVICEEVENT = 0xB;
public const uint SERVICE_CONTROL_HARDWAREPROFILECHANGE = 0xC;
public const uint SERVICE_CONTROL_POWEREVENT = 0xD;
public const uint SERVICE_CONTROL_SESSIONCHANGE = 0xE;

public const uint SERVICE_STOPPED = 0x1;
public const uint SERVICE_START_PENDING = 0x2;
public const uint SERVICE_STOP_PENDING = 0x3;
public const uint SERVICE_RUNNING = 0x4;
public const uint SERVICE_CONTINUE_PENDING = 0x5;
public const uint SERVICE_PAUSE_PENDING = 0x6;
public const uint SERVICE_PAUSED = 0x7;
public const int LG_SLEEP_TIME = 4000;

[DllImportAttribute("advapi32.dll")]
public static extern bool ControlService(IntPtr hSCObject, uint control,
    [MarshalAs(UnmanagedType.Struct)] ref SERVICE_STATUS status);

[DllImportAttribute("advapi32.dll")]
public static extern bool CloseServiceHandle(IntPtr hSCObject);

public const uint SERVICE_NO_CHANGE = 0xFFFFFFFF; //-1
public const uint SERVICE_KERNEL_DRIVER = 0x1;
public const uint SERVICE_FILE_SYSTEM_DRIVER = 0x2;
public const uint SERVICE_WIN32_OWN_PROCESS = 0x10;
public const uint SERVICE_WIN32_SHARE_PROCESS = 0x20;
public const uint SERVICE_INTERACTIVE_PROCESS = 0x100;
public const uint SERVICETYPE_NO_CHANGE = SERVICE_NO_CHANGE;

public const uint SERVICE_BOOT_START = 0x0;
public const uint SERVICE_SYSTEM_START = 0x1;
public const uint SERVICE_AUTO_START = 0x2;
public const uint SERVICE_DEMAND_START = 0x3;
public const uint SERVICE_DISABLED = 0x4;
public const uint SERVICE_STARTTYPE_NO_CHANGE = SERVICE_NO_CHANGE;



public const uint SERVICE_ERROR_IGNORE = 0x0;
public const uint SERVICE_ERROR_NORMAL = 0x1;
public const uint SERVICE_ERROR_SEVERE = 0x2;
public const uint SERVICE_ERROR_CRITICAL = 0x3;
public const uint SERVICE_ERROR_CONTROL_NO_CHANGE = SERVICE_NO_CHANGE;

[DllImportAttribute("advapi32.dll")]
public static extern IntPtr CreateService(
    IntPtr hSCManager, string lpServiceName, string lpDisplayName,
    uint dwDesiredAccess, uint dwServiceType,
    uint dwStartType, uint dwErrorControl, string lpBinaryPathName,
    string lpLoadOrderGroup, int lpdwTagId, string lpDependencies,
    string lpServiceStartName, string lpPassword);

public int CreateDriver(string cszDriverName, string cszDriverFullPath)
{
    IntPtr schManager;
    IntPtr schService;
    SERVICE_STATUS svcStatus;
    bool bStopped = false;
    int i;
    if (string.IsNullOrEmpty(cszDriverName) || string.IsNullOrEmpty(cszDriverFullPath))
        return -1;
    schManager = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
    if (schManager == IntPtr.Zero)
        return -1;
    schService = OpenService(schManager, cszDriverName, SERVICE_ALL_ACCESS);
    if (schService != IntPtr.Zero)
    {
        svcStatus = new SERVICE_STATUS();
        if (ControlService(schService, SERVICE_CONTROL_INTERROGATE, ref svcStatus))
        {
            if (svcStatus.dwCurrentState != SERVICE_STOPPED)
            {
                if (!ControlService(schService, SERVICE_CONTROL_STOP, ref svcStatus))
                {
                    CloseServiceHandle(schService);
                    CloseServiceHandle(schManager);
                    return -1;
                }
                for (i = 0; i < 10; i++)
                {
                    if (!ControlService(schService, SERVICE_CONTROL_INTERROGATE,
                        ref svcStatus) || svcStatus.dwCurrentState == SERVICE_STOPPED)
                    {
                        bStopped = true;
                        break;
                    }

                    System.Threading.Thread.Sleep(LG_SLEEP_TIME);
                }
                if (!bStopped)
                {
                    CloseServiceHandle(schService);
                    CloseServiceHandle(schManager);
                    return -1;
                }
            }
        }
        CloseServiceHandle(schService);
        CloseServiceHandle(schManager);
        return 0;
    }
    schService = CreateService(schManager, cszDriverName, cszDriverName,
        SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_SYSTEM_START,
        SERVICE_ERROR_NORMAL, cszDriverFullPath, null, 0, null, null, null);
    if (schService == IntPtr.Zero)
    {
        CloseServiceHandle(schManager);
        return -1;
    }
    CloseServiceHandle(schService);
    CloseServiceHandle(schManager);
    return 0;
}

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有