如何进行“公开声明 Ansi 功能”在 Visual Basic 6.0 中?
我正在尝试将此 VB.NET / C# 声明转换为 Visual Basic 6.0 声明,但遇到了麻烦(包括 C# 版本,转换为 VB.NET 不是问题):
[DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
private static extern int UrlMkSetSessionOption(
int dwOption,
string pBuffer,
int dwBufferLength,
int dwReserved);
如您所见,在 Visual Basic/C# 中,我们有 CharSet=CharSet.Ansi
部分,我不知道如何在 Visual Basic 6.0 中执行此操作 - 我尝试在别名末尾添加 A...Alias “UrlMkSetSessionOptionA”
...但这不起作用(说在 urlmon.dll 中找不到 DLL 入口点
)。如果没有这个,发送到 pBuffer 的字符串就会变成乱码(我无法识别的奇怪字符)。
这是我到目前为止所得到的......
Public Declare Sub UrlMkSetSessionOption Lib "urlmon.dll" (ByVal _
dwOption As Long, _
pBuffer As Any, _
ByVal dwBufferLength As Long, _
ByVal dwReserved As Long)
I'm trying to convert this VB.NET / C# declaration into a Visual Basic 6.0 one, having trouble (included is the C# version, converting to VB.NET not a problem):
[DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
private static extern int UrlMkSetSessionOption(
int dwOption,
string pBuffer,
int dwBufferLength,
int dwReserved);
As you can see, in Visual Basic/C# we have that CharSet=CharSet.Ansi
part, which I don't know how to do in Visual Basic 6.0 - I tried adding the A at the end of the Alias Name... Alias "UrlMkSetSessionOptionA"
... but that didn't work (says can't find DLL entrypoint in urlmon.dll
). Without this, the string sent to pBuffer is coming out as gibberish (weird characters I can't recognize).
Here is what I've gotten so far...
Public Declare Sub UrlMkSetSessionOption Lib "urlmon.dll" (ByVal _
dwOption As Long, _
pBuffer As Any, _
ByVal dwBufferLength As Long, _
ByVal dwReserved As Long)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将 VB6 函数声明为
Sub
,使编译器/解释器查找void
函数(也称为过程),而实际函数具有int< /code> 返回类型。
如果您将包含语句更改为以下内容,它应该可以工作:
您可能还必须将所有出现的
Long
替换为Integer
,但由于我缺乏我不确定VB6的经验。You declare the VB6 function as
Sub
, making the compiler/interpreter look for avoid
function (also known as procedure), while the actual function has anint
return type.It should work if you change your include statement to this:
It may be that you'll also have to replace all occurences of
Long
withInteger
, but due to my lack of experience with VB6 I'm not sure.我刚刚发现声明是正确的,并且需要有一种特殊的方式来调用它 - 基本上你需要将字符串作为 ByVal 传递 - 它只是在我尝试组合不同的东西时随机工作。感谢大家的贡献。这是声明为子函数时的调用。
我希望这对某人有用 - 当您在没有“ByVal strUA”的情况下调用第二个参数并仅传递“strUA”时,内部函数必须假定 ByRef,这意味着它正在尝试使用我们传递给它的变量(ANSI Visual Basic 6.0 STRING),当然,当它这样做时,它最终会变成乱码,因为 C 函数使用的 string 类型不是 ANSI Visual Basic 字符串类型。
因此,当将其作为 ByVal 传递时,它只是按值(而不是按引用)传递它,然后可以使用与其使用的字符串类型兼容的自己的变量/数据类型组合。我希望这对某人有帮助。
I've just figured out that the declaration is correct, and there was a particular way that it needed to be called - basically you need to pass the string as ByVal - it just randomly worked while I was trying a combination of different things. Thank you for everyone for their contribution. Here is the call if declared as a sub.
I hope this is useful to someone - when you call the second argument without the "ByVal strUA" and just pass "strUA" the internal function must assume ByRef, which means it is trying to use the variable we passed it (an ANSI Visual Basic 6.0 STRING), and of course, when it does this, it ends up as gibberish as the string type the C function uses is not an ANSI Visual Basic string type.
So, when passing it as ByVal it just passes it by value (not by reference) and can then use its own variable/datatype combination which is compatible with the string type it uses. I hope that helps someone.
私有声明函数 UrlMkSetSessionOption Lib "urlmon" (ByVal dwOption As Long, ByVal pBuffer As String, ByVal dwBufferLength As Long, ByVal dwReserved As Long) As Integer
公共函数 ChangeUserAgent
End Function
Private Declare Function UrlMkSetSessionOption Lib "urlmon" (ByVal dwOption As Long, ByVal pBuffer As String, ByVal dwBufferLength As Long, ByVal dwReserved As Long) As Integer
Public Function ChangeUserAgent
End Function