试图从VB调用C DLL。无法得到其中一个参数 [英] Trying to call a C DLL from VB. Can't get one of the parameters working

查看:127
本文介绍了试图从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 Convention type 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.

这是我有的:

公共声明函数InitPowerUSB LibPwrUSBDll.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,string,integer,long等等。它似乎不想运行。

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.

但是,坏DLL调用约定错误通常意味着您有一个 CDecl entrypoint和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装饰器: p>

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-code时也不会工作。 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 array ByRef / code>除非这是一个Unicode DLL entrypoint。函数返回值也很可能 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天全站免登陆