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

这是一个错误:

A call to PInvoke function 'Test!Test.Form2::SendMessage' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

有没有人有解决这个问题的想法? 感恩

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 和 return 值是指针大小。请注意,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。这提供了更大的灵活性。