DLL erstellen und in ein Projekt einbinden

Andrej Dyck

Mitglied
Hallo zusammen,

Ich habe schon danach gesucht und nicht viel Hilfreiches gefunden.
Ich suche ein Tutorial, dass beschreibt wie man DLL's erstellt und dann in einem anderen c++ Projekt einbindet und die Funktionen nutzt. Ich arbeite mit Micrsoft Visual Studio 2005 (C++). Gibt es eine allgemeine Vorgehensweise, oder macht das jeder Compiler anders?
Bitte postet ein Link zu einem Tut. oder wenn ihr Zeit habt auch ein kleines Beispiel Programm.

Gruß Andrej

P.S. Bitte lasst die Option "COM" aus oder überzeugt mich mit Vor- und Nachteilen.
 
/*dllmain.cpp*/

/*******************************************************************************

IGOR ( Userexit für das EASY Archivsystem)
Version 1.3


Geprüft auf dem EASY Archivsystem 3.40
Plattform Microsoft Windows
**************************************
Erstellt unter DEV C++ 4.9.9.2
( Bloodshed Software )
*************************************/

// Bibliotheken einbinden
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <string>


#define DLL_EXPORT
#include "dll.h"


extern "C"
{


DECLDIR int Zaehler1_alter_Stand (
int nType, int nState, int nFldIdx,
LPSTR lpszMskName, int nSizeMskName,
LPSTR lpszBlobFile, int nSizeBlobFile,
LPSTR lpszFldData, int nSizeFldData,
LPSTR lpszErrText, int nSizeErrText
)
{

char *str;
char fd[200];
char pfad[200]="IGOR\\";
str = "_zaehlerstand_1_alt.txt";
char acUserName[100];

DWORD nUserName = sizeof(acUserName);
if (GetUserName(acUserName, &nUserName))

{

strcat(acUserName,str);
strcpy (fd,acUserName);
strcat(pfad,fd);


FILE *cs;
cs=fopen(pfad,"a+");

if (cs==NULL)
{system ("md IGOR");}

if (cs!=NULL)
{
fclose(cs);
}

remove(pfad);

FILE *dt;
dt=fopen(pfad,"a+");

if (dt!=NULL)
{
fprintf(dt,"%s", lpszFldData );
fclose (dt);
}

else
{
MessageBox (
NULL, "FC2",
"EASY CAPTURE - USEREXIT", MB_OK|MB_ICONSTOP
);
}


}

else
{
MessageBox (
NULL, "FC1",
"EASY CAPTURE - USEREXIT", MB_OK|MB_ICONSTOP
);
}

return 0;
}





/*dll.h*/

/*******************************************************************************

IGOR ( Userexit für das EASY Archivsystem)
Version 1.3


Geprüft auf dem EASY Archivsystem 3.40
Plattform Microsoft Windows
**************************************
Erstellt unter DEV C++ 4.9.9.2
( Bloodshed Software )
*************************************/
#ifndef _DLLTUT_DLL_H_
#define _DLLTUT_DLL_H_

// DLL EX - / IM - port halt
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

// Prototyp
extern "C"
{

DECLDIR int Zaehler1_alter_Stand
(int nType, int nState, int nFldIdx,
LPSTR lpszMskName, int nSizeMskName,
LPSTR lpszBlobFile, int nSizeBlobFile,
LPSTR lpszFldData, int nSizeFldData,
LPSTR lpszErrText, int nSizeErrText
);

}

#endif
 
wie du eine dll erstellst weißt du aber schon?
wenn nicht hier mal schnell einen kleinen quellcode.
das einbinden macht sich am besten wenn du es pers system (""); machst, dann schaut er nähmlich in win32 system nach :-d


/*dllmain.cpp*/

/*******************************************************************************

IGOR ( Userexit für das EASY Archivsystem)
Version 1.3


Geprüft auf dem EASY Archivsystem 3.40
Plattform Microsoft Windows
**************************************
Erstellt unter DEV C++ 4.9.9.2
( Bloodshed Software )
*************************************/

// Bibliotheken einbinden
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <string>


#define DLL_EXPORT
#include "dll.h"


extern "C"
{


DECLDIR int Zaehler1_alter_Stand (
int nType, int nState, int nFldIdx,
LPSTR lpszMskName, int nSizeMskName,
LPSTR lpszBlobFile, int nSizeBlobFile,
LPSTR lpszFldData, int nSizeFldData,
LPSTR lpszErrText, int nSizeErrText
)
{

char *str;
char fd[200];
char pfad[200]="IGOR\\";
str = "_zaehlerstand_1_alt.txt";
char acUserName[100];

DWORD nUserName = sizeof(acUserName);
if (GetUserName(acUserName, &nUserName))

{

strcat(acUserName,str);
strcpy (fd,acUserName);
strcat(pfad,fd);


FILE *cs;
cs=fopen(pfad,"a+");

if (cs==NULL)
{system ("md IGOR");}

if (cs!=NULL)
{
fclose(cs);
}

remove(pfad);

FILE *dt;
dt=fopen(pfad,"a+");

if (dt!=NULL)
{
fprintf(dt,"%s", lpszFldData );
fclose (dt);
}

else
{
MessageBox (
NULL, "FC2",
"EASY CAPTURE - USEREXIT", MB_OK|MB_ICONSTOP
);
}


}

else
{
MessageBox (
NULL, "FC1",
"EASY CAPTURE - USEREXIT", MB_OK|MB_ICONSTOP
);
}

return 0;
}





/*dll.h*/

/*******************************************************************************

IGOR ( Userexit für das EASY Archivsystem)
Version 1.3


Geprüft auf dem EASY Archivsystem 3.40
Plattform Microsoft Windows
**************************************
Erstellt unter DEV C++ 4.9.9.2
( Bloodshed Software )
*************************************/
#ifndef _DLLTUT_DLL_H_
#define _DLLTUT_DLL_H_

// DLL EX - / IM - port halt
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

// Prototyp
extern "C"
{

DECLDIR int Zaehler1_alter_Stand
(int nType, int nState, int nFldIdx,
LPSTR lpszMskName, int nSizeMskName,
LPSTR lpszBlobFile, int nSizeBlobFile,
LPSTR lpszFldData, int nSizeFldData,
LPSTR lpszErrText, int nSizeErrText
);

}

#endif
 
Um eine DLL unter C/C++/C# einzubinden, brauchts gar nicht so viel Code:

#include <windows.h>
#include <stdio.h>

// Definition der DLL-Funktion, die verwendet werden soll
typedef double (*AddNumbers)(double, double);

int main () {
AddNumbers function;
double result;

// DLL Datei laden
HINSTANCE hinstLib = LoadLibrary("MyDll.dll");

if (hinstLib) {
// Die Einsprungadresse abfragen
function = (AddNumbers) GetProcAddress(hinstLib, "AddNumbers");

// Die Funktion aufrufen
if (function)
result = (*function) (1,2);
// result ist der Rückgabewert der Funktion
// Die DLL-Datei wieder entladen
BOOL fFreeResult = FreeLibrary(hinstLib);
}

// Das Ergebnis anzeigen
if (!hinstLib || !function)
printf("Fehler: Konnte die Funktion nicht aufrufen\n");
else
printf("Das Ergebnis ist: %f\n", result);
return 0;
}

Das reicht

Lorenz
 
Abgesehen davon, dass das Thema vier Jahre alt ist, könnte man auch einfach die lib beim Kompilieren miteinbinden; so erspart man sich die ganze Laderei

Und C# hat rein gar nichts mit C und C++ zu tun, ausser dem Buchstaben im Namen.
Dlls werden auch anders eingebunden.
 

Neue Beiträge

Zurück