如何进行“公开声明 Ansi 功能”在 Visual Basic 6.0 中?

发布于 2024-12-29 10:02:00 字数 824 浏览 3 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷情妓 2025-01-05 10:02:00

您将 VB6 函数声明为 Sub,使编译器/解释器查找 void 函数(也称为过程),而实际函数具有 int< /code> 返回类型。

如果您将包含语句更改为以下内容,它应该可以工作:

Public Declare Function UrlMkSetSessionOption Lib "urlmon.dll" (ByVal _
    dwOption As Long, _
    pBuffer As Any, _
    ByVal dwBufferLength As Long, _
    ByVal dwReserved As Long) As Long

您可能还必须将所有出现的 Long 替换为 Integer,但由于我缺乏我不确定VB6的经验。

You declare the VB6 function as Sub, making the compiler/interpreter look for a void function (also known as procedure), while the actual function has an int return type.

It should work if you change your include statement to this:

Public Declare Function UrlMkSetSessionOption Lib "urlmon.dll" (ByVal _
    dwOption As Long, _
    pBuffer As Any, _
    ByVal dwBufferLength As Long, _
    ByVal dwReserved As Long) As Long

It may be that you'll also have to replace all occurences of Long with Integer, but due to my lack of experience with VB6 I'm not sure.

任谁 2025-01-05 10:02:00

我刚刚发现声明是正确的,并且需要有一种特殊的方式来调用它 - 基本上你需要将字符串作为 ByVal 传递 - 它只是在我尝试组合不同的东西时随机工作。感谢大家的贡献。这是声明为子函数时的调用。

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

我希望这对某人有用 - 当您在没有“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.

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

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.

木槿暧夏七纪年 2025-01-05 10:02:00
Const URLMON_OPTION_USERAGENT = &H10000001
Const URLMON_OPTION_USERAGENT_REFRESH = &H10000002

私有声明函数 UrlMkSetSessionOption Lib "urlmon" (ByVal dwOption As Long, ByVal pBuffer As String, ByVal dwBufferLength As Long, ByVal dwReserved As Long) As Integer

公共函数 ChangeUserAgent

(Optional ByVal MyUserAgent As String = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36")

 Call UrlMkSetSessionOption(URLMON_OPTION_USERAGENT_REFRESH, vbNullString, 0, 0)

 Call UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, MyUserAgent, LenB(MyUserAgent), 0)

End Function

Const URLMON_OPTION_USERAGENT = &H10000001
Const URLMON_OPTION_USERAGENT_REFRESH = &H10000002

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

(Optional ByVal MyUserAgent As String = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36")

 Call UrlMkSetSessionOption(URLMON_OPTION_USERAGENT_REFRESH, vbNullString, 0, 0)

 Call UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, MyUserAgent, LenB(MyUserAgent), 0)

End Function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文