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

BarryTang
BarryTang used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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.

Author

Commented:
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

Commented:
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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
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

Commented:
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.

Author

Commented:
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 ?

Commented:
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.

Author

Commented:
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
Commented:
Ok, I do not know what the function does, and I don't know the variable declarations you may be using.  The thing is that you must correct the Declare statement as I gave it to you, then you must use Option Explicit to avoid datatype problems and force you to dim every variable.  Finally, you must use the function as I mentioned, i. e. using the VarPtr() function on a string datatype variable for the second parameter.  If you do not do it like this, the C++ code will not work correctly.

Example:

'The following goes in the General declarations section of the module
Option Explicit

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



'Now the following function exemplifies the use of the function
Public Function Test() as long

Dim str1 as string
Dim str2 as string
dim num1 as long
dim num2 as long

    str1 = <add your test data>
    str2 = <add your test data for the second parameter>
    num1 = <the number desired goes here>
    num2 = <the other number goes here>
    test = testa(str1, varptr(str2), num1, num2)
end function

The above should make the function work correctly and please note that it is of PARAMOUNT importance that you declare all your variables to be of the proper datatype.  If you don't, the function may return a bogus return value, or make your program crash!

Author

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial