日期:2014-05-18  浏览次数:21086 次

求求高手帮我把下面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 h