Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
all compiled fine but after run, the error occurred 100%
in visual studio 2008 but vc6.
How can to modify it to pass runtime?
What I want is that to run fine only with *.exe and *.dll
Thank you
//test_dll.cpp
#include <windows.h>
#include <stdio.h>
typedef int (*PFUN)(int);
void main()
{
HMODULE hModule = ::LoadLibrary(L"DllTest.dll");
PFUN newfun = (PFUN)::GetProcAddress(hModule,"fun");
int i = newfun(2);
printf("The result is %d",i);
::FreeLibrary(hModule);
}
/***************************************/
//win32_dll1.h
extern "C" __declspec(dllexport) int fun(int i)
/**************************************/
//win32_dll1.def
EXPORTS
fun
/**************************************/
//win32_dll1.cpp
#include "win32_dll1.h"
int fun(int i)
{
return i*i;
}
Posted
Updated 29-Oct-10 3:45am
v2
Comments
waertf.chen 29-Oct-10 11:10am    
thank you Nishant help me to resolve it

You are not checking for errors. You need to check and see that LoadLibrary succeeded, GetProcAddress succeeded etc.
 
Share this answer
 
//test_dll.cpp
#include <windows.h>
#include <stdio.h>
typedef int (__cdecl *MYPROC)(int);

void main()
{
HMODULE  hmoudLib = LoadLibrary(TEXT("win32_dll1.dll"));
MYPROC ProcAdd  = (MYPROC) GetProcAddress(hmoudLib, "fun");
int i =ProcAdd(2);
printf("The result is %d",i); //The result is 4
::FreeLibrary(hmoudLib);
}
/***************************************/
//win32_dll1.h
extern "C" __declspec(dllexport) int fun(int i)
/**************************************/
//win32_dll1.def
EXPORTS
fun
/**************************************/
//win32_dll1.cpp
#include "win32_dll1.h"
int fun(int i)
{
return i*i;
}
 
Share this answer
 

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