Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-03-01
10
Medium Priority
?
288 Views
Last Modified: 2010-05-03
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
0
Comment
Question by:BarryTang
  • 5
  • 5
10 Comments
 
LVL 7

Expert Comment

by:webJose
ID: 8048205
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.
0
 

Author Comment

by:BarryTang
ID: 8051987
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

0
 
LVL 7

Expert Comment

by:webJose
ID: 8052079
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:BarryTang
ID: 8057900
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

0
 
LVL 7

Expert Comment

by:webJose
ID: 8061449
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.
0
 

Author Comment

by:BarryTang
ID: 8064628
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 ?

0
 
LVL 7

Expert Comment

by:webJose
ID: 8068503
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.
0
 

Author Comment

by:BarryTang
ID: 8078248
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
0
 
LVL 7

Accepted Solution

by:
webJose earned 600 total points
ID: 8081763
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!
0
 

Author Comment

by:BarryTang
ID: 8141881
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
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question