vb.net 应用程序重新加载 INI 文件
Posted
技术标签:
【中文标题】vb.net 应用程序重新加载 INI 文件【英文标题】:vb.net applications to reload the INI file 【发布时间】:2017-12-21 22:28:45 【问题描述】:我编写此代码来更改 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 签名的调用约定和参数是否与目标非托管签名匹配。
有没有人想办法解决这个问题? 感激不尽
【问题讨论】:
www.pinvoke.net @Plutonix 推荐这样的网站没什么意义,因为它充满了错误。 【参考方案1】: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 位值,wParam
、lParam
和返回值是指针大小。注意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
。这提供了更大的灵活性。
【讨论】:
以上是关于vb.net 应用程序重新加载 INI 文件的主要内容,如果未能解决你的问题,请参考以下文章