如何做“公开声明Ansi功能"在 Visual Basic 6.0 中? [英] How to do "Public Declare Ansi Function" in Visual Basic 6.0?

查看:23
本文介绍了如何做“公开声明Ansi功能"在 Visual Basic 6.0 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此 VB.NET/C# 声明转换为 Visual Basic 6.0 声明,但遇到了问题(包括 C# 版本,转换为 VB.NET 不是问题):

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);

如您所见,在 Visual Basic/C# 中,我们有 CharSet=CharSet.Ansi 部分,我不知道在 Visual Basic 6.0 中如何做 - 我尝试在别名的结尾... Alias "UrlMkSetSessionOptionA" ... 但这不起作用(说 在 urlmon.dll 中找不到 DLL 入口点).没有这个,发送到 pBuffer 的字符串就会变成乱码(我无法识别的奇怪字符).

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)

推荐答案

我刚刚发现声明是正确的,并且有一种特殊的方式需要调用它 - 基本上你需要将字符串作为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.

UrlMkSetSessionOption URLMON_OPTION_USERAGENT, ByVal strUA, Len(strUA), 0

我希望这对某人有用 - 当您在没有ByVal strUA"的情况下调用第二个参数并仅传递strUA"时,内部函数必须假定为 ByRef,这意味着它正在尝试使用我们传递给它的变量(一个ANSI Visual Basic 6.0 STRING),当然,当它这样做时,它最终会像 C 函数使用的 string 类型不是 ANSI Visual Basic 字符串类型一样胡言乱语.

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.

因此,当将它作为 ByVal 传递时,它只是通过值(而不是通过引用)传递它,然后可以使用它自己的变量/数据类型组合,它与它使用的字符串类型兼容.我希望对某人有所帮助.

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.

这篇关于如何做“公开声明Ansi功能"在 Visual Basic 6.0 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆