Calling a C dll in VB6

Hello,
I am new to using DLL in VB.
In C dll the function is declared as follows-

     main(char argv1[], char argv2[], char argv3[], char argv4[])

Call from VB-
     Private Declare Function main Lib "y:\PHU\phu.dll" (ByVal str1 As String, ByVal str2 As String, ByVal str3 As String, ByVal str4 As String) As Long

But I am not getting the correct output.(Like I want 6391 n I get 6705)
Is this calling convention correct?

Or C dll is coverting 2 variables as follows-

         inx = atof(argv2);
      iny = atof(argv3);

If answer is wrong due to these statements, how can I correct them?
Thanks in advance for any help.
Regards.
ashu_a90Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
skyDaemonConnect With a Mentor Commented:
Everything is fine,but can you tell me what is the use of
#define MODULE _T("My")

Ha,

Sorry, force of habit.  We use that in our error handling here.  MODULE will get logged as part of a call stack to tell you where you were when problems occurred.  Feel free to ignore.

As for how to return an array.  I would have suggested looking up SAFEARRAY, but it sounds like you're writing in basic C (I'm not sure it supports it).  Worst case scenario you could return a big delimited string to vb instead of an array. (eg, "Value1|Value2|...").  Afraid I couldn't tell you the best way to do it from C without spending some time testing alternatives.

Good luck,
0
 
skyDaemonCommented:
Don't convert the argv2 etc directly.  Copy argv2 into another char* variable, and THEN convert to a float.

ie,

pchVar2 = argv2;
inx = atof(pchVar2);

pchVar3 = argv3;
iny = atof(pchVar3);


0
 
ashu_a90Author Commented:
skyDaemon,
Yeah this could be true.
Actually this is a 3rd party DLL made to be used in AVENUE and it is working perfectly in it, now I want to use it in VB6. I only have the .C file.

Can you tell me how to again convert it into DLL after editing it,I have never made a DLL.I tried to find out on web but ended up so many weird things.

What should be written in header file?
Stepwise...

thank you.
Regards
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ashu_a90Author Commented:
Or Could it controlled from VB without modifying the code of DLL.

Regards.
0
 
skyDaemonCommented:
Ok.  

First, you need to make a dll project in whatever you're going to use to compile your C code.  I use Visual C++ 6.

Then add your .c file and two header .h files to your project.

You also need to define _MY_DLL for the project.  In VC++ you right click on the project and select settings.  Then go to the C/C++ tab and on the "preprocessor definitions" line you enter an item for _MY_DLL.

In the first header file declare a couple things for your dll.  Call this header file dllbase.h, you'll need to include it in your other header file (see below).

#ifdef _MY_DLL
#define MyDllItem __declspec( dllexport )
#define MODULE _T("My")
#else
#define MyDllItem __declspec( dllimport )
#endif

#endif

In the second header file you need standard C header declarations for all of your classes and functions in the dll.  The header declarations nearly match what's in the .c file.  You also need to have a line in the extern "C" section for each function you want to be externally visible.  Your .c file needs to have a #include statement which includes this second header.

#include dllbase.h

extern "C"
{
  MyDllItem long main(BSTR str1, BSTR str2, BSTR str3,
                      BSTR str4);
}

class MyDllItem CMyClass
{
  long main( BSTR str1, BSTR str2, BSTR str3, BSTR str4 );
  ...
}

Then you need to compile your new dll project.

Hope that helps some.  At the least it should give you material to look up.



0
 
ashu_a90Author Commented:
I am getting an error--
syntax error:'string'

at the line where I have written extern "C".
How to resolve this.
Regards.
0
 
ashu_a90Author Commented:
Hey,
I removed the extern "C" statement and it worked perfectly all right.
Even there is no problem with
atof(argv2)...etc.

Instead of making two .h files I made only one.
Everything is fine,but can you tell me what is the use of
#define MODULE_T("My")

And is instead of using BSTR str1...I used char argv[].
Now I want to return an array from the DLL.
How can I receive it in VB.
Cheers.
0
 
ashu_a90Author Commented:
Hey,
I am compiling it with VC++6.0 compiler,I think I can also use C++ in it.
Can you please tell me how to use SAFEARRAY,for it we will have to make it as MFC dll...or what.
I want to return a 2D array of type float.

I am able to pass the pointer to first element of array to VB but I could not figure out how to retrieve the value from it and also subsequent values.

Regards.
I have increased the points too.
0
 
ashu_a90Author Commented:
Hey I was able to return the Char array from dll to VB.

I have to pass the variable as an argument in dll , then I assing value to that variable using wsprinf() function.

And I am able to retrieve that in VB

Thank Skydeamon for you advices.
Cheers.
0
 
ashu_a90Author Commented:
I am giving full points for my first question which you answered very well.
Thanx
0
All Courses

From novice to tech pro — start learning today.