简体   繁体   English

从C调用VB6中制作的DLL

[英]calling a DLL made in VB6 from C

Okay, this one is the reverse of the last question I struggled with... I feel like I'm sooo close to getting it but it's just not working. 好的,这是我遇到的最后一个问题的反面……我觉得我已经快要解决了,但它没有用。 Before, I was trying to compile a DLL in C, and then call it from VB, but I realized that's not really what I want (I want the program to be written in C, while using a VB frontend, not the frontend being the program and using the functionality of a DLL). 以前,我试图用C编译DLL,然后从VB调用它,但我意识到这并不是我想要的(我希望程序使用C编写,同时使用VB前端,而不是前端是程序并使用DLL的功能)。

So now, I'm compiling a DLL in VB and trying to call it from a C .exe. 所以现在,我正在VB中编译一个DLL,并尝试从C .exe调用它。

"But wait, you can't compile a DLL with VB!" “但是等等,您不能使用VB编译DLL!” Actually , I intercepted VB6.EXE's call to the linker, and added a definition file for the functions I want it to export, and here's the best part--if I copy the dll to system32, I can call the functions from another VB program and it works fine. 实际上 ,我拦截了对链接器的VB6.EXE调用,并为我要导出的功能添加了一个定义文件,这是最好的部分-如果将dll复制到system32,则可以从另一个VB程序调用这些功能而且效果很好。 Only thing is, I can't seem to load it from C. Well, I can, and I get the handle, and I get the address of the functions, it's just the program fails. 唯一的事情是,我似乎无法从C加载它。好了,我可以了,我得到了句柄,我得到了函数的地址,只是程序失败了。

Here's the dummy function I exported: 这是我导出的虚拟函数:

Public Function PopupMessage(ByVal rawr As Integer) As Integer
    Msgbox "HERE BE " & rawr & " DRAGON(S)."
    PopupMessage = 4
End Function

I can call it like this from another VB6 program: 我可以从另一个VB6程序中这样称呼它:

Public Declare Function PopupMessage Lib "vbdll" _
    (ByVal howmanydragons As Integer) As Integer

Private Sub Command1_Click()
    If IsNumeric(Text1.Text) Then
        Text2.Text = PopupMessage(Text1.Text)
    End If 
End Sub

and, from C: 并且,从C:

int main(){
  typedef short int (*vbfun)(short int); //VB6 "Integer" is 16 bits
  vbfun popup_message;
  HANDLE dllhnd;
  dllhnd = LoadLibrary("vbdll.dll");
  if(dllhnd>(void*)HINSTANCE_ERROR){
    popup_message = (vbfun)GetProcAddress(dllhnd, "PopupMessage");
    printf("%d", popup_message(3));
  }
  return 0;
}

when I debug, I find out that I'm getting an access violation which, in the past has meant that I forgot ByVal in the VB function, and it was passing a reference to 5 or something. 当我调试时,我发现我遇到了访问冲突,这过去意味着我在VB函数中忘记了ByVal,并且它传递了对5的引用。 But I can't see anything wrong here... I get the ProcAddress from the DLL which matches the offset I get from dumpbin, I can call it from VB, but the C caller is getting an access violation... 但是我在这里看不到任何错误...我从DLL中获得了ProcAddress,它与我从dumpbin中获得的偏移量匹配,我可以从VB中调用它,但是C调用者遇到访问冲突...

Any ideas? 有任何想法吗? Anyone see something that I've overlooked? 有人看到我忽略了的东西吗? I realize I'm doing something pretty obscure and mostly unheard of, instead of just learning how to design a front end in C, but now I'm determined to fix it. 我意识到我正在做一些相当模糊的事情,而且几乎是闻所未闻的,而不是仅仅学习如何在C语言中设计前端,但是现在我决心修复它。

Just use the free add-in vbAdvance . 只需使用免费的插件vbAdvance即可 It lets you create standard DLLs that are callable from C (rather than COM DLLs). 它使您可以创建可从C调用的标准DLL(而不是COM DLL)。

It also gives you access to many other advanced build features and many IDE convenience features. 它还使您可以访问许多其他高级构建功能和许多IDE便利功能。 Create console apps, create a DllMain entry point in your DLLs, XP Manifest compiler for XP styles, support Terminal Server, etc. 创建控制台应用程序,在DLL中创建DllMain入口点,用于XP样式的XP Manifest编译器,支持终端服务器等。

(No offence, but you are reinventing the wheel here, not breaking new ground! I linked to vbAdvance on this answer too.) (没有冒犯,但您是在这里重塑方向,而不是开拓新天地!我在此答案上也链接到vbAdvance。)

You have to initialize VB6 run-time manually in your exported function, prior to using any function in the run-time. 您必须先在导出的函数中手动初始化VB6运行时,然后才能在运行时中使用任何功能。 Check out the Compact In-Process Multi-threading sample, the ThreadEntry function shows how to do it. 查看紧凑型进程内多线程示例, ThreadEntry函数显示了如何执行此操作。

You would have to call it through the COM interface, no? 您将必须通过COM接口调用它,不是吗? Something more like this . 更像这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM