繁体   English   中英

vb.net应用程序重新加载INI文件

[英]vb.net applications to reload the INI file

我编写此代码来更改Windows中的默认打印机并可以正常工作,但是在重新加载INI文件时出现错误,这是一个代码:

 Private Sub SetDefaultPrinter(ByVal PrinterName As String, ByVal DriverName As String, ByVal PrinterPort As String)
    Dim DeviceLine As String

    'rebuild a valid device line string
    DeviceLine = PrinterName & "," & DriverName & "," & PrinterPort

    'Store the new printer information in the
    '[WINDOWS] section of the WIN.INI file for
    'the DEVICE= item
    Call WriteProfileString("windows", "Device", DeviceLine)

    'Cause all applications to reload the INI file
    Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows")

End Sub

Private Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lparam As String) As Long
Private Const HWND_BROADCAST As Long = &HFFFF&
Private Const WM_WININICHANGE As Long = &H1A

这是一个错误:

调用PInvoke函数'Test!Test.Form2 :: SendMessage'已使堆栈不平衡。 这可能是因为托管PInvoke签名与非托管目标签名不匹配。 检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配。

有谁有解决这个问题的想法? 感谢

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    ByVal lparam As String) As Long

您的数据类型错误。 hwnd参数的大小是指针大小, wMsg是32位值, wParamlParam和返回值是指针大小。 请注意, Long是64位类型。

相反,它应该是

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, _
    ByVal lparam As String) As IntPtr

请注意,我建议在现代使用pinvoke声明而不是Declare 这提供了更大的灵活性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM