Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a function in a C project

Status
Not open for further replies.

BaDi

Programmer
May 14, 2002
32
0
0
NL
Hi!

Can someone please tell me how to call a function in a C-project.

I know that I have to declare a function like:
Declare Function Test Lib "testlib" Alias "TestFunction" (ByVal teststring As String) As string

But how do fill this in.. I read something about using ANSI characters for external communication. But how do I place the function in a library and how do I communicate with it.

I need to call the function and put the return value in a variable in VB.

Thanx in advance!
 
Hi,

You have to compile your C function into a dll and register it. When that is done you can call it from VB with you declare statement. I'm not an expert in this, but have made a few vc++ dlls and used then in VB, so I could show an example if you are interested.
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
I am interested!

Thanx in advance!

 
Ok Here goes....

Open VC++, select new -> win 32 dynamic Link-library under the projects tab.
Copy the follwing to text files and add them to the project:

dlltest.cpp
-----------------------------------------------------------
double _stdcall MyMul( double A, double B,long No )
{
double Dummy;

for (long i=1;i<No+1;i++)
{
Dummy = A*B;
}
return(Dummy);

}; // end of MyMul
-----------------------------------------------------------
dlltest.def
-----------------------------------------------------------
LIBRARY dlltest
DESCRIPTION DLL TEST

EXPORTS
MyMul @1
-----------------------------------------------------------

Compile the project. Place the dll in your windows system32 directory (that actually not necessary, but I like it that way). Goto to the command promt and register the dll:
regsvr32 dlltest.dll

Now try the function from VB:
-----------------------------------------------------------
Private Declare Function MyMul Lib &quot;dlltest.dll&quot; (ByVal A As Double, ByVal B As Double, No As Long) As Double


Dim A As Double, B As Double, Dummy As Double, No As Long

A = 7: B = 7: No = 10000000
Dummy = MyMul1(A, B, No)
-----------------------------------------------------------

Good luck [pipe] Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Thanx!

But is it possible that if I have a huge project full of headers and source files that I just open that specific file with that specific function which I need in that huge project and add this:

LIBRARY dlltest
DESCRIPTION DLL TEST

EXPORTS
MyMul @1

at the end of the function?

And do I need to compile the whole project then.. or just the source file?

Thanx in advance!
 
Hi,

As I said, I'm not an expert in C++, but you'll have to make sure that you function can stand alone (e.i. that is not uses other part of the program ) or make sure that you include the part that it uses.
MyMul should be changed to the name of the function. and
LIBRARY dlltest
DESCRIPTION DLL TEST

EXPORTS
MyMul @1
is the contents of dlltest.def and not a part of the source code file.

If you haven't tried this before I suggest that you play around with my example until that works, before you try it on your project.
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top