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

x
?
Solved

Calling a C dll in VB6

Posted on 2003-03-21
10
Medium Priority
?
197 Views
Last Modified: 2010-05-01
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.
0
Comment
Question by:ashu_a90
[X]
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
10 Comments
 
LVL 2

Expert Comment

by:skyDaemon
ID: 8184658
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
 

Author Comment

by:ashu_a90
ID: 8187607
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
 

Author Comment

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

Regards.
0
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!

 
LVL 2

Expert Comment

by:skyDaemon
ID: 8198466
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
 

Author Comment

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

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

Author Comment

by:ashu_a90
ID: 8204627
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
 
LVL 2

Accepted Solution

by:
skyDaemon earned 160 total points
ID: 8205286
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
 

Author Comment

by:ashu_a90
ID: 8207233
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
 

Author Comment

by:ashu_a90
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.
Cheers.
0
 

Author Comment

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

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