A question re VCC DLL called from VB 
Author Message
 A question re VCC DLL called from VB

I have created a VCC ++ project which builds a DLL.

I need to call the DLL from VB.

I declared the dll, I added extern C and finally I could use the
exported functions.

The problem is with C functions accepting parameters other than int
and functions which return values other than int.

In all those cases I get bad dll calling convention.

It runs fine as exe but not from within VB environment (I need finally
to convert  the code to OCX, and it failed as OCX as well)

Can someone show me an example of C code returning something other
than int (preferable char *.)

I have tried sending for example a string as parameter to the c
function.
I could put value into it in the c code by strcpy, but only when
running it as exe.

I tried putting byval and byref, as all FAQ explains but still same
problem



Wed, 26 Dec 2001 03:00:00 GMT  
 A question re VCC DLL called from VB
Here is some code I have used when teaching VB that should answer your
question.

VB Code:

' The following goes in a Module
Declare Function NetGetDCNameWrap Lib
"c:\classes\CDll\DLLDemo\Debug\DLLDemo" _
    (ByVal sDomainName As String, _
     ByVal sPDCComputerName As String) As Long

' The following is in a form
Private Sub Command1_Click()
    Static strDomainName As String * 255
    Static strPDCComputerName As String * 255
    Dim rc As Long

    strDomainName = "PDC" & vbNullChar
    strPDC = vbNullChar

    rc = 0
    rc = NetGetDCNameWrap(strDomainName, strPDCComputerName)

    Text1.Text = strPDCComputerName
    Text2.Text = Format(rc, "###0")
End Sub

C Code:

#include <windows.h>
#include <lm.h>

#define MAX_SERVER_LENGTH 18

static LPWSTR ByteToWide(char *in, LPWSTR out, int lenout);
static char *WideToByte(LPWSTR in, char *out, int lenout);

extern "C"  int  APIENTRY
DllMain (HINSTANCE hInst,
 DWORD ul_reason_for_call,
 LPVOID lpReserverd)
{
    return 1;

Quote:
}

//--------------------------------------------------------------------------
-
//   Purpose: Get name of primary domain controller
//   Input: DomainName = null-terminated string of domain name
//   Output: PDCName = fixed-length string of primary domain controller
//   Returns: return code of NetGetDCName
//   Calls: NetGetDCName
//
extern "C" long _stdcall
NetGetDCNameWrap (char *DomainName, char *PDCName)
{
    WCHAR wszDomainName[256];
    WCHAR *ui = NULL;
    NET_API_STATUS status;

    ByteToWide(DomainName,
       wszDomainName,
    sizeof(wszDomainName));

    status = NetGetDCName(NULL,
         wszDomainName,
               (LPBYTE *) &ui);

    WideToByte(ui,
    PDCName,
    MAX_SERVER_LENGTH);

 NetApiBufferFree((LPBYTE *) &ui);

 return(status);

Quote:
}

//--------------------------------------------------------------------------
-
//   Local functions
//
static LPWSTR ByteToWide(char *in, LPWSTR out, int lenout)
{
    int rc;
    rc = MultiByteToWideChar(CP_ACP,
        MB_PRECOMPOSED,
        in,
        -1,
        out,
        lenout);
 return out;

Quote:
}

static char *WideToByte(LPWSTR in, char *out, int lenout)
{
    WideCharToMultiByte(CP_ACP,
      WC_COMPOSITECHECK,
      in,
      -1,
      out,
      lenout,
      NULL,
      NULL);
    char *p;
    p = out;

    while (*p != '\0')
        p++;

    *p = ' ';  // blank out null terminator
    return out;

Quote:
}



Quote:
> I have created a VCC ++ project which builds a DLL.

> I need to call the DLL from VB.

> I declared the dll, I added extern C and finally I could use the
> exported functions.

> The problem is with C functions accepting parameters other than int
> and functions which return values other than int.

> In all those cases I get bad dll calling convention.

> It runs fine as exe but not from within VB environment (I need finally
> to convert  the code to OCX, and it failed as OCX as well)

> Can someone show me an example of C code returning something other
> than int (preferable char *.)

> I have tried sending for example a string as parameter to the c
> function.
> I could put value into it in the c code by strcpy, but only when
> running it as exe.

> I tried putting byval and byref, as all FAQ explains but still same
> problem



Wed, 26 Dec 2001 03:00:00 GMT  
 A question re VCC DLL called from VB
Use the _stdcall convention for your C functions.  It's required for
functions called from VB.  It will "decorate" the function names, so you may
need a DEF file as well.

--
     Jim Mack
     MicroDexterity, Inc

     http://www.microdexterity.com




Quote:
> I have created a VCC ++ project which builds a DLL.

> I need to call the DLL from VB.

> I declared the dll, I added extern C and finally I could use the
> exported functions.

> The problem is with C functions accepting parameters other than int
> and functions which return values other than int.

> In all those cases I get bad dll calling convention.

> It runs fine as exe but not from within VB environment (I need finally
> to convert  the code to OCX, and it failed as OCX as well)

> Can someone show me an example of C code returning something other
> than int (preferable char *.)

> I have tried sending for example a string as parameter to the c
> function.
> I could put value into it in the c code by strcpy, but only when
> running it as exe.

> I tried putting byval and byref, as all FAQ explains but still same
> problem



Thu, 27 Dec 2001 03:00:00 GMT  
 A question re VCC DLL called from VB
Well, I finally found a way.

It made no change if I used _stdcall or other approach, I get only
different error messages.

What I finally did was as follows
in C I used of course extern "C" for the function.

In VB I added to the declare alias .... with function name and the

It will be easier to show it:

The C function declaration was

SNMPDLL1_API fnSnmpdll1(char *DomainName, char *tt)

where

#define SNMPDLL1_API __declspec(dllexport)

The VB code declare was
Declare Sub fnSnmpdll1 Lib "d:\snmp\build\snmpdll1\debug\snmpdll1.dll"


I am not sure I fully understand the mechanism, but it works.

Quote:

>Use the _stdcall convention for your C functions.  It's required for
>functions called from VB.  It will "decorate" the function names, so you may
>need a DEF file as well.



Thu, 27 Dec 2001 03:00:00 GMT  
 A question re VCC DLL called from VB
Ron -
    You must use _stdcall, there is no alternative.  If you find something
that "works" without it, all you're doing is delaying the inevitable crash.
Clearly you are using _stdcall, whether intentionally or not, because the
routine name is decorated with _stdcall prefix and suffix.  You can deal
with this decoration by using an alias (as you show), or you can use a DEF
file as I suggested in another post.  When you use a DEF file you don't need
to concern yourself with aliases.

--
     Jim Mack
     MicroDexterity, Inc

     http://www.microdexterity.com




Quote:
> Well, I finally found a way.

> It made no change if I used _stdcall or other approach, I get only
> different error messages.

> What I finally did was as follows
> in C I used of course extern "C" for the function.

> In VB I added to the declare alias .... with function name and the

> It will be easier to show it:

> The C function declaration was

> SNMPDLL1_API fnSnmpdll1(char *DomainName, char *tt)

> where

> #define SNMPDLL1_API __declspec(dllexport)

> The VB code declare was
> Declare Sub fnSnmpdll1 Lib "d:\snmp\build\snmpdll1\debug\snmpdll1.dll"


> I am not sure I fully understand the mechanism, but it works.


> >Use the _stdcall convention for your C functions.  It's required for
> >functions called from VB.  It will "decorate" the function names, so you
may
> >need a DEF file as well.



Thu, 27 Dec 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. VB 5.0 calling C++ 5.0 DLL that calls winsock.dll

2. Newbie question - trying to call fortran dll from VB

3. Question: Calling my own DLL from VB

4. Compiling a C++ code to build a .DLL using VB Calls Question

5. Calling a method in a vb dll from html page - newbie question

6. a question about calling DLL from VB

7. How Do I Call (Pascal Calling Convention) DLLS in VB

8. 2 PRBS: Calling 16 bit DLL from VB5 and Bad DLL Calling Convention

9. Bad DLL calling convention VB-to-C DLL

10. Calling Std C DLL from VB ActiveX DLL

11. VB DLL calling another C DLL

12. Call VB DLL or COM object from within VB COM object or EXE

 

 
Powered by phpBB® Forum Software