DLL call made in VB6 from C

Ok, this is one of the references to the last question I was wrestling with ... I feel like I'm that close to getting it, but it just doesn't work. I used to try to compile a DLL in C and then call it from VB, but I realized that I actually don't want that (I want the program to be written in C, when using the VB interface, not as a program interface and using DLL functionality).

So now I am compiling the DLL to VB and trying to call it from C.exe.

"But wait, you can't compile a DLL with VB!" In fact , I intercepted the VB6.EXE call to the linker and added a definition file for the functions I want to export to it, and here are the best parts - if I copy the dll to system32 I can call the functions from another VB program and it works fine. The only thing is that I can't boot from C. Well I can and I get the handle and I get the address of the functions, it's just the program doesn't work.

Here's a 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:

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:

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 i get an access violation which in the past meant i forgot ByVal in VB function and it was passing reference to 5 or something. But I don't see anything wrong here ... I am getting a ProcAddress from a DLL that matches the offset I get from dumpbin, I can call it from VB, but the caller C gets an access violation ...

Any ideas? Does anyone see what I have forgotten? I realize that I am doing something rather obscure and mostly unheard of, instead of just learning how to design an interface in C, but now I decided to fix it.

0


a source to share


3 answers


Just use the free vbAdvance add- on . It allows you to create standard DLL files that can be called from C (not from COM libraries).

It also gives you access to many other advanced build features and many IDE functionality. Build console applications, create DllMain entry point in your DLLs, XP Manifest compiler for XP styles, support Terminal Server, etc.



(No offense, but you are reinventing the wheel here, not breaking new ground! I linked with vbAdvance on this answer ).

+2


a source


Before using any function at runtime, you must initialize the VB6 runtime manually . Check out the compact In-Process Multi-threading , the function ThreadEntry

shows you how.



+2


a source


You will need to call through the COM interface, no? Something like this .

+1


a source







All Articles