如何从 VB.Net 或 C# 传递 LPCSTR * 以使用 ObtainUserAgentString
How to pass LPCSTR * from VB.Net or C# to use ObtainUserAgentString
这是我的代码,我收到错误的输入参数
Imports System.Runtime.InteropServices
Module Module1
Private Declare Function ObtainUserAgentString Lib "urlmon.dll" (ByVal dwOption As Integer, ByRef pcszUAOut As String, ByRef cbSize As Integer) As Integer
Sub Main()
Dim c As String
Dim ptr As IntPtr = Marshal.StringToHGlobalUni(c)
Dim useragent As String
Dim size As Integer
Dim result As Integer
result = ObtainUserAgentString(0, useragent, size)
'Try
'result = ObtainUserAgentString(0, ptr, size)
'Finally
' Marshal.FreeHGlobal(ptr)
' 'End Try
End Sub
End Module
Reference source 显示 ObtainUserAgentString
函数定义为:
c#
[DllImport(ExternDll.Urlmon, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
private static extern MS.Internal.Interop.HRESULT ObtainUserAgentString(int dwOption, StringBuilder userAgent, ref int length);
vb.net
<DllImport(ExternDll.Urlmon, ExactSpelling:=True, CharSet:=System.Runtime.InteropServices.CharSet.Ansi, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Private Shared Function ObtainUserAgentString(dwOption As Integer, userAgent As StringBuilder, ByRef length As Integer) As MS.Internal.Interop.HRESULT
End Function
所以你的问题的答案是StringBuilder。
这是我的代码,我收到错误的输入参数
Imports System.Runtime.InteropServices
Module Module1
Private Declare Function ObtainUserAgentString Lib "urlmon.dll" (ByVal dwOption As Integer, ByRef pcszUAOut As String, ByRef cbSize As Integer) As Integer
Sub Main()
Dim c As String
Dim ptr As IntPtr = Marshal.StringToHGlobalUni(c)
Dim useragent As String
Dim size As Integer
Dim result As Integer
result = ObtainUserAgentString(0, useragent, size)
'Try
'result = ObtainUserAgentString(0, ptr, size)
'Finally
' Marshal.FreeHGlobal(ptr)
' 'End Try
End Sub
End Module
Reference source 显示 ObtainUserAgentString
函数定义为:
c#
[DllImport(ExternDll.Urlmon, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
private static extern MS.Internal.Interop.HRESULT ObtainUserAgentString(int dwOption, StringBuilder userAgent, ref int length);
vb.net
<DllImport(ExternDll.Urlmon, ExactSpelling:=True, CharSet:=System.Runtime.InteropServices.CharSet.Ansi, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Private Shared Function ObtainUserAgentString(dwOption As Integer, userAgent As StringBuilder, ByRef length As Integer) As MS.Internal.Interop.HRESULT
End Function
所以你的问题的答案是StringBuilder。