Link to home
Start Free TrialLog in
Avatar of BarryTang
BarryTang

asked on

DLL Function can run in exe but fail to run in VB6 environment ?

I have a VB6 program that call a function in a DLL written by VC++ which return a string.

Actually, the function return nothing ( not success ) when I run in a simple testing program in VB environment. However when I make it in exe file, it can run !!!

Moreover, when I paste the code in a more complex VB6 program and build exe, it fails to run again !!!

Can anyone face with this kind of problem so can give me some hint/reference for it, thank you.

Barry
Avatar of webJose
webJose

Well, I haven't seen anything like that, but if you show the C++ code for the function and the Declare statement you are using, I may be able to help.
Avatar of BarryTang

ASKER

Hello, webJose :

The declaration in VB6 is something like that :

Declare Function testa Lib "testa.dll" (ByVal filename As String, ByRef TString As String, ByVal chn As Integer, ByVal mnote As Integer) As Boolean

However, the C++ code is very large in size and some parts related to something that cannot not disclose. I do need help but I know it may be very hard for you to help me like this. So if possible, can you give me some hint or refer link for me to search. Or I paste some part of the program that you think it is the key for the error with some hiding.

Thanks for help anyway !

Barry

Seeing the second parameter I presume VB may be crashing every time your press F5.  Show the C++ function prototype as I believe is a problem declaring the arguments, but to correct I must check the parameters to the function.
Hello, webJose :

After my checking with the parameter, I face some strange thing as follows :

DLL function declare as follows :

Declare Function testa Lib "testa.dll" (ByVal filename As String, ByRef TString As String, ByVal chn As Integer, ByVal mnote As Integer) As Boolean

Originally, I have the invoke code as follows :

  x = testa(tfile1, tstr1, val(mid(y,2,1)), 1)

It function return false ( not crash ), and tstr1 get nothing. But when I change the code as follows :

  z = val(mid(y,2,1))
  x = testa(tfile1, tstr1, z, 1)

The function return true and the tstr1 get correct value.
Do you get any explanation for it ?

The parameters of C exported function is something like that :
extern "C"
int __stdcall TESTA(LPVOID byvalStringFname, LPVOID byrefTString, int channel, int mnote)

Barry

Ok, I see the C function prototype is using void pointers.  That is no good for me :-(.  In order to find the problem, you will have to show me the function coding, or explain to me this:

How are the pointers interpreted?

The whole key may be in there.
This function coding as follows :

extern "C"
int __stdcall TESTA(LPVOID byvalStringFname, LPVOID byrefTString, int channel, int mnote)
{
     char * pszFname = (char*)byvalStringFname;
     BSTR bstrT= *(BSTR*)byrefTString;
  char info[1000];
  char* caption = "testing";
  int count = 0;

    char* pszT = testa(pszFname, channel, ANY, mnote);

    int len = 0;
    if (pszT)
      len = strlen(pszT);
    pszT = (char*)realloc(pszT, len+2);
    if (len == 0)
      pszT[0] = pszT[1] = '\0'; // 00
    else
      pszT[len+1] = '\0';

    bstrT = SysAllocStringByteLen(pszT, len+2);
 
    *(BSTR*)byrefTString = bstrT;
    if (!bstrT)
      return 0;
     
    return len;
}

Can this be enough info to find out problem ?

Ok, since you are not using a type library, you must change your declare statement as follows:

Declare Function testa Lib "testa.dll" (ByVal filename As String, byval TString As Long, ByVal chn As long, ByVal mnote As long) As Boolean

Notice I also changed the integers to be longs.  Under Win32, an int is a 32-bit value.

Now, to use this function, you must call it like this:

x = testa(tfile1, varptr(tstr1), val(mid(y,2,1)), 1)

Note the use of the VarPtr function.  This change is mandatory as VB will automatically convert all BSTRs to ANSI BSTRs when passed as arguments of functions in dlls, that is, unless you use a type library to define the functions.

Hopefully, the above changes will do the work ok.
Hello, webJose :

I try your suggestion but regret to tell you that it does not work : the function return false in either VB environment and exe file !

However, I find some strange things accidently as follows :

Originally, the follows does not work
z1=val(mid(y,2,1))
x = testa(tfile1, tstr1, z1, z2)

but when I change z1 to string type, that is :

z1=mid(y,2,1)
z2=ltrim(str(z2))
x = testa(tfile1, tstr1, z1, z2)

the function works !!! (not expect, strange !)

Do you have any idea on this thing ?

Barry
ASKER CERTIFIED SOLUTION
Avatar of webJose
webJose

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello, webJose :

Actually, I still cannot solve my problem in some case.
For example, the dll works in some program but not work in another program. However, I still accept your answer since you already offer me some hints ( as my original requirement ).
If you have any other hints/link, please also state.
Thank you very much !!!

Barry