PDA

Click to See Complete Forum and Search --> : Dll


WylerWolf
Mar 5th, 2001, 04:17 PM
Can someone be nice and explain me how to make a dll with MS Visual C++ 6 (step by step)

I know C++, Im a newbie with vb but i know how to call funtion from a alredy make dll (like mcisendstring in winmm) so be nice and explain to me :)

parksie
Mar 5th, 2001, 04:30 PM
Firstly, you create a new project "Win32 DLL", not an MFC one. Call it "testdll" (for simplicity). Add a new blank source file called "testdll.cpp", and add the following code:

long __stdcall TestFunction(long lInput) {
return lInput + 5;
}

Now, the only thing that's different here is the __stdcall part. This tells it which way to call the function. This calling convention is the one used by the Windows API since it's simpler to program for.

Next, you need to add a .DEF file. This can just be a source file again, but call it "testdll.def". Add the following:

LIBRARY "testdll.dll"

EXPORTS
TestFunction

This tells the linker what functions to export from the DLL for usage by other programs.

Compile this and hopefully you should have a DLL.

The definition for this function is:

Declare Function TestFunction Lib "testdll" (ByVal lInput As Long) As Long

WylerWolf
Mar 5th, 2001, 04:33 PM
thanks man!

WylerWolf
Mar 5th, 2001, 05:00 PM
the .def file where do I put it ?
-source
-header
-ressource
or it doesn't matter it juste for the form ?

parksie
Mar 5th, 2001, 05:10 PM
It needs to be in the project somewhere. It doesn't really go under any of those categories so put it in the root.

WylerWolf
Mar 5th, 2001, 05:14 PM
ok thx

WylerWolf
Mar 5th, 2001, 07:15 PM
ok and why when I write this :

double __stdcall Barre( double Now, double Max)
double Barre;
if ((Max != 0) && (Now <= Max) {
Barre = Now / Max;
return Barre;
}
else
{
return 0;
}
}


they give to me these error ?
C:\Benoit\Informatique\WylerTeam\WTDll\WTDll.cpp(2) : warning C4518: 'double ' : storage-class or type specifier(s) unexpected here; ignored
C:\Benoit\Informatique\WylerTeam\WTDll\WTDll.cpp(2) : error C2146: syntax error : missing ';' before identifier 'Barre'
C:\Benoit\Informatique\WylerTeam\WTDll\WTDll.cpp(2) : fatal error C1004: unexpected end of file found

why I can't use double ?

crptcblade
Mar 5th, 2001, 10:02 PM
double __stdcall Barre( double Now, double Max)
{ <--- Try adding a brcket here, see if that helps...
double Barre;
if ((Max != 0) && (Now <= Max) {
Barre = Now / Max;
return Barre;
}
else
{
return 0;
}
}

The Hobo
May 7th, 2002, 01:57 PM
I'm getting errors when I try this:


#include <string>
using namespace std;

string __stdcall period(string t) {

t = t + '.';

return t;
}


Errors:
Linking...
vbshelfex.def : error LNK2001: unresolved external symbol Characters
Debug/vbshelfex.lib : fatal error LNK1120: 1 unresolved externals
LINK : fatal error LNK1141: failure during build of exports file
Error executing link.exe.

The Hobo
May 7th, 2002, 01:58 PM
Nevermind. I named the function wrong in the .def file.

But now it crashes in VB when I try to use it.

parksie
May 7th, 2002, 02:16 PM
VB doesn't understand C++ strings, which are actually a specific class.

To pass strings back to VB, you need to go back to string buffers (or use the COM strings which I'm sure CB or someone else will pop in with)...

http://www.parksie.net/StringDLL.zip

CornedBee
May 10th, 2002, 05:14 AM
Yeah, the VB native string is the BSTR.

A function that returns a string:

BSTR __stdcall RetString()
{
return SysAllocString(L"Hello, man!");
}


Note the L before the string! It is absolutely necessary and requires you to get familiar with the concept of UNICODE strings.

The Hobo
May 10th, 2002, 11:04 AM
Thanks...

SeaCapnJ
Sep 15th, 2003, 11:25 AM
I did the following as shown, except I named it sbuff. When I implemented this in the vb app, I could not get the value I sent to be incremented by 5. Any suggestions?


sbuff.cpp:
long __stdcall TestFunction(long lInput) {
return lInput + 5;
}


sbuff.def:
LIBRARY "sbuff.dll"

EXPORTS
TestFunction



Declare Function TestFunction Lib "sbuff" (ByVal lInput As Long) As Long

CornedBee
Sep 16th, 2003, 01:49 AM
What's your VB code?

SeaCapnJ
Sep 16th, 2003, 10:05 AM
I was just stupid. Thx cornbee.

forgot to say i = testfunction(i)



:o