Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Calling a C dll in VB6

Posted on 2003-03-21
Medium Priority
Last Modified: 2010-05-01
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.
Question by:ashu_a90
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3

Expert Comment

ID: 8184658
Don't convert the argv2 etc directly.  Copy argv2 into another char* variable, and THEN convert to a float.


pchVar2 = argv2;
inx = atof(pchVar2);

pchVar3 = argv3;
iny = atof(pchVar3);


Author Comment

ID: 8187607
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?

thank you.

Author Comment

ID: 8188557
Or Could it controlled from VB without modifying the code of DLL.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 8198466

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")
#define MyDllItem __declspec( dllimport )


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.


Author Comment

ID: 8199606
I am getting an error--
syntax error:'string'

at the line where I have written extern "C".
How to resolve this.

Author Comment

ID: 8204627
I removed the extern "C" statement and it worked perfectly all right.
Even there is no problem with

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.

Accepted Solution

skyDaemon earned 160 total points
ID: 8205286
Everything is fine,but can you tell me what is the use of
#define MODULE _T("My")


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,

Author Comment

ID: 8207233
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.

I have increased the points too.

Author Comment

ID: 8248147
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.

Author Comment

ID: 8248155
I am giving full points for my first question which you answered very well.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

722 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