试图从 VB 调用 C DLL.无法使参数之一工作 [英] Trying to call a C DLL from VB. Can't get one of the parameters working

查看:13
本文介绍了试图从 VB 调用 C DLL.无法使参数之一工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在尝试设置 USB 电源板.

Trying to set up a USB power strip.

这是文档:

Initializes the Power USB API.

Name: InitPowerUSB 

Parameters: model:returns the model number(1:basic, 2:digIO, 3:watchdog, 4:Smart), firmware: returns firmware version in ?.? format in a character string (major revision and minor revision) 

Return: >0 if successful. Returns number of PowerUSB devices connected

C++ Example:

if (!m_pwrUSBInit)
{
    int model; char firmware[8];
    if ((ret=InitPowerUSB(&model, firmware)) > 0)
    {
        m_pwrUSBInit = 1;
        m_numDevices = ret;
    }
}

我一直在尝试使用我的 VB6 代码进行大约一个小时的工作,但没有运气.程序要么崩溃,要么显示错误,如 Bad Calling Dll Conventiontype mismatch 等.

I have been trying to get this working with my VB6 code for around an hour now with no luck. The program either crashes, displays an error like Bad Calling Dll Convention, type mismatch, et cetera.

这是我所拥有的:

Public Declare Function InitPowerUSB Lib "PwrUSBDll.dll" (ByRef model As Integer, ByVal firmware As String) As Integer

Dim model As Integer
model = 0

Dim firmware As String
firmware = ""

If (InitPowerUSB(model, firmware)) > 0) Then

EndIf

我尝试将固件更改为字节数组、byref、字符串、整数、长整数等.它似乎不想运行.

I have tried changing firmware to byte arrays, byref, string, integer, long, etc. It just doesn't seem to want to run.

有人知道这个问题的解决方案吗?谢谢

Does anyone know of a solution to this problem? Thanks

推荐答案

我无法回答您的函数签名问题的其余部分,因为我没有您的 PwrUSBDll.dll 的任何文档.

I can't answer the rest of your function signature woes since I don't have any documentation for your PwrUSBDll.dll.

然而,Bad DLL calling convention"错误通常意味着您有一个 CDecl 入口点,而 VB6 只能在一些帮助下调用这些入口点.

However "Bad DLL calling convention" errors generally mean you have a CDecl entrypoint and VB6 can only call those with some help.

有几个修复.

显而易见的一个是修改源代码并使用 StdCall 重新编译该 DLL.

The obvious one is to modifiy the source and recompile that DLL using StdCall instead.

另一种方法是为该 DLL 创建一个类型库,这有助于通知 VB6 并解决该问题.

Another is to create a type library for that DLL, which helps inform VB6 about the issue and resolves it.

然后您可以选择使用 VB6 的未记录的 CDecl 装饰器:

Then you have the option of using VB6's undocumented CDecl decorator:

Public Declare Function InitPowerUSB CDecl Lib "PwrUSBDll.dll" ( _
    ByRef model As Integer, _
    ByVal firmware As String) As Integer

但是缺点是在 IDE 中运行时这将不起作用,编译为 p 代码时也不起作用.p-code 解释器不处理这个关键字.

However the downside is that this will not work when run within the IDE, nor will it work when compiled to p-code. The p-code interpreter doesn't process this keyword.

因此,您可以在 IDE 运行中绕过它并提供虚拟结果进行测试,或者您可以在 VB6 中创建一个小型包装 DLL,然后将其单独编译为本机代码.

So you could just bypass it in IDE runs and supply dummy results for testing, or you can create a small wrapper DLL in VB6 that you separately compile to native code.

注意事项:

为了解决您的问题,我们必须假设您在该参数列表中传递了正确的数据类型.C++ int 是 VB6 Long.您最好为该 char[8] 传递一个 VB6 Byte 数组 ByRef,除非这是一个 Unicode DLL 入口点.函数返回值也很可能是Long.

For this to solve your problem we'd have to assume you are passing correct data types in that argument list. A C++ int is a VB6 Long. You are probably better off passing a VB6 Byte array ByRef for that char[8] unless this is a Unicode DLL entrypoint. The function return value is also most likely Long.

这篇关于试图从 VB 调用 C DLL.无法使参数之一工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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