RegSetValueEx dll调用约定错误

warning: 这篇文章距离上次修改已过566天,其中的内容可能已经有所变动。

RegSetValueEx dll调用约定错误

   很久没写vb的项目了,最近要做个屏幕监控系统,随时对公司里的电脑进行截屏,其实就是监控员工的上网行为,够狠吧!软件采用c/s模式,考虑兼容性,客户端采用vb6开发,实现开机自启动,按远程的配置参数进行截屏。说到开机自动启动,就想到了注册表的runonce,于是就有了本文的错误解决方案。

   先看网上抄来的代码。vb就是有这个好处,你想到的问题在百度基本上都能找到了。

Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long        ' Note that if you declare the lpData parameter as String, you must pass it By Value.

Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_CURRENT_USER = &H80000001

Dim a
Private Sub Command1_Click()
writeKey
a = 0
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub

Private Sub Form_Load()
On Error Resume Next
MkDir "c:\zt" '½¨Îļþ¼Ð
Me.AutoRedraw = True
End Sub

Private Sub Timer1_Timer()
a = a + 1
If a = 3 Then 'ÈýÃëºó¿ªÊ¼×¥ÆÁ
Me.Hide
BitBlt Me.hDC, 0, 0, Screen.Width, Screen.Height, _
GetDC(GetActiveWindow), 0, 0, vbSrcCopy '×¥ÆÁ
Me.Show
Dim sFile As String
sFile = "C:\zt\" & Format(Now, "yyyymmddhhmmss") & ".BMP"
SavePicture Me.Image, sFile '±£´æMe.hDC
Timer1.Enabled = False
End If
End Sub

 Sub writeKey()
 Dim keyhand As Long
Dim r As Long
'Dim strString As String
strString = App.Path & "\" & App.EXEName & ".exe"
r = RegCreateKey(HKEY_LOCAL_MACHINE, "software\microsoft\windows\currentversion\run", keyhand)
 
r = RegSetValueEx(keyhand, "capScreen", 0, 1, ByVal strString, Len(strString))
r = RegCloseKey(keyhand)

 End Sub
  在调用时,老是弹出dll调用约定错误,我明明是按api的参数传值的,怎么会错捏! 但出错时还是能set到value,这是怪事哈。百度了一下,似乎别人的都没遇到过这种问题,哈哈,我遇到ufo了。于是不断地调试,把传入的string改短,问题依旧,搞到凌晨3点,百思不得其解,再仔细观察了api声明,发现有个any,觉得不顺眼,就把它改成string,成功啦,setvalue非常正常,难道vb的any类型是挂在那看的? 我没深究下去了,估计是我电脑是win7系统?。 另外,有提示参数‘类型错误’也是这个any惹的,改改就行啦。 再嗦一下,注册表路径如果是中文,会乱码,我传入的.exe会变成.nmi之类的,改成英文路径即ok

none
最后修改于:2023年05月08日 07:55

评论已关闭