Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hola estoy aprendiendo a crear una DLL y luego utilizarla, C++ este es mi primer ejercicio. Lo estoy probando en línea de comandos "cl.exe". EL resultado es realizar operaciones básicas. En la línea de comandos escribo
Terminal
cl /c dllmain.cpp
Esto para crear un archivo objeto.
Terminal
cl dllmain.obj /link /DLL /OUT:dllmain.dll
Esto para crear un DLL
Terminal
cl EjecutaDLL.cpp /c /I "directorio de la DLL"
Esto para empezar a usar la DLL.

Archivo dllmain.cpp
C++
#include "dll.h"
#include <windows.h>

DllClass::DllClass()
{

}

DllClass::~DllClass()
{

}


BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
	switch(fdwReason)
	{
		case DLL_PROCESS_ATTACH:
		{
			break;
		}
		case DLL_PROCESS_DETACH:
		{
			break;
		}
		case DLL_THREAD_ATTACH:
		{
			break;
		}
		case DLL_THREAD_DETACH:
		{
			break;
		}
	}
	
	/* Return TRUE on success, FALSE on failure */
	return TRUE;
}

extern "C" __declspec(dllexport)

double suma(double a, double b) {
	return a + b;
}
extern "C" __declspec(dllexport)

 double resta(double a, double b) {
	return a - b;
}
extern "C" __declspec(dllexport)
double multiplicar(double a, double b) {
	return a * b;
}
extern "C" __declspec(dllexport)

double dividir(double a, double b) {
	return a / b;
}


Archivo dll.h
C++
<pre>#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
#define DLLIMPORT __declspec(dllexport)
#else
#define DLLIMPORT __declspec(dllimport)
#endif

class DLLIMPORT DllClass
{
	public:
		DllClass();
		virtual ~DllClass();
		void HelloWorld();
};

#endif


EjecutarDLL.cpp
C++
#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

//definimos los punteros para las funciones de la dll
typedef double(WINAPI *op)(double, double);
typedef double(WINAPI *op1)(double, double);
typedef double(WINAPI *op2)(double, double);
typedef double(WINAPI *op3)(double, double);

int main(){

op  suma;
op1 resta;
op2 multiplicacion;
op3 division;

HINSTANCE LibDll;

LibDll = LoadLibrary("Operaciones.dll");   //cargamos nuestra dll

    //verificamos si cargo la dll
    if(LibDll != NULL){
            //Cargamos las funciones de la dll
            suma = (op)GetProcAddress(LibDll, "suma");
            resta = (op1)GetProcAddress(LibDll, "resta");
            multiplicacion = (op2)GetProcAddress(LibDll, "multiplicar");
            division = (op3)GetProcAddress(LibDll, "dividir");
            
            //Verificamos si encontra las funciones
            if(suma != NULL){
                   cout << "NO SE ENCONTRO LA FUNCION";
            }else{
                    
                    //Si lo encontra mostramos las operaciones
                    cout << "La Suma es:  "  << suma(2, 4) << "\n";
                    cout << "La Resta es:  "  << resta(2, 4) << "\n";
                    cout << "La Multiplicacion es:  "  << multiplicacion(2, 4) << "\n";
                    cout << "La Division es:  "  << division(2, 4) << "\n";

            }

            //Liberamos Memoria de la DLL
            FreeLibrary(LibDll);
            LibDll = NULL;

    }
    else{

            cout<< "ERROR AL CARGAR LA DLL";
    }
    return 0;
}


What I have tried:

En internet a visto que Esto se debe a que el proyecto utiliza otra versión de C++. Pero la verdad no me queda muy claro. Me pueden ayudar.
Posted
Updated 1-Jan-23 2:17am
v2
Comments
Richard MacCutchan 1-Jan-23 3:19am    
This is an English language site, please translate your question so we may all understand it.

As far as I can see you did not define BUILDING_DLL when you compiled the dll code. See Creating and Using a Dynamic Link Library (C+) | Microsoft Learn[^].
 
Share this answer
 
v2
Comments
merano99 1-Jan-23 7:39am    
+5. The messages disappear as soon as the functions are declared with "dllexport".
Richard MacCutchan 1-Jan-23 7:53am    
Thank you.
Richard has already described the first obvious error. The error messages with the inconsistent DLL bindings disappear as soon as the functions are declared with "dllexport".

After that there can still be problems with the path. While it is often sufficient in the development environment to specify a relative path, such as:
C++
LibDll = LoadLibrary(TEXT("./../x64/Debug/MyDll.dll")); //cargamos nuestra dll

it would be better to get a reliable path e.g. with GetModuleFileName().

There is still a small error at the top when loading. It should be of course:
C
suma = (op)GetProcAddress(LibDll, "suma");
if (suma == NULL) { /*Error*/ }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900