|
-
Jul 15th, 2002, 09:03 PM
#1
Calling a Dll
Hello!
I made a dll :
PHP Code:
#include "MYDLL.h"
__declspec( dllexport ) int doubleInt(int iNumber)
{
return (iNumber * 2);
}
and a .DEF
Code:
LIBRARY MYDLL
DESCRIPTION "Simply double an integer"
EXPORTS
doubleInt @1
but now that's what I just found in some thread here but that doesn't work and I know that I miss something but I really do not have a clue how to call my DLL in an other C++ project 
Here is what I got :
PHP Code:
#include <iostream>
//Prototype
void LoadDll(void);
void main(void)
{
LoadDll();
}//End main
void LoadDll(void)
{
HMODULE hDLL = LoadLibrary("MYDLL");
if (hDLL)
MYFUNC function = (MYFUNC)GetProcAddress(hDLL, "MyFunction");
else
std::cout << "Error loading DLL";
}//End LoadDll
Any one can help me ?
-
Jul 15th, 2002, 09:05 PM
#2
Here is all error :
Code:
error C2065: 'HMODULE' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'hDLL'
error C2065: 'hDLL' : undeclared identifier
error C2065: 'LoadLibrary' : undeclared identifier
error C2065: 'MYFUNC' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'function'
error C2065: 'function' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'GetProcAddress'
error C2065: 'GetProcAddress' : undeclared identifier
error C2181: illegal else without matching if
-
Jul 16th, 2002, 02:20 AM
#3
PowerPoster
Try this...
Your DLL source file:
PHP Code:
#include "MYDLL.h"
int __declspec( dllexport ) doubleInt(int iNumber)
{
return (iNumber * 2);
}
Your .DEF file:
PHP Code:
LIBRARY "MYDLL.dll"
DESCRIPTION "Simply double an integer"
EXPORTS
doubleInt
Almost make sure your DLL header file is also right.
Now, the code you're testing your DLL with:
PHP Code:
#include <iostream>
#include <windows.h> 'Must if you're using LoadLibrary, etc
//Prototype
void LoadDll(void);
//Must declare a function pointer type you want
typedef void (*MYFUNCTION)(int);
void main(void)
{
LoadDll();
}//End main
void LoadDll(void)
{
'HMODULE is old. Use HINSTANCE instead
HINSTANCE hDLL = LoadLibrary("MYDLL.dll"); 'Extension
if (hDLL)
MYFUNCTION myfunc= (MYFUNCTION)GetProcAddress(hDLL, "MyFunction"); //Shouldn't it be doubleInt?
//You can free the DLL here just for performance
//FreeLibrary(hDLL);
else
std::cout << "Error loading DLL";
}//End LoadDll
Make sure the path to DLL is correct and naming of all the files is also correct.
Might be some typos in the above code though.
-
Jul 16th, 2002, 08:28 AM
#4
Yes but how can i use the doubleInt function ?
I tryed that in the main :
PHP Code:
void main(void)
{
LoadDll();
int a = 5;
a = myfunc(a);
std::cout << a;
}//End main
but that doesn't work
-
Jul 16th, 2002, 12:02 PM
#5
Monday Morning Lunatic
You have to assign it to a function pointer, then call through the pointer:
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 16th, 2002, 01:52 PM
#6
Why that doesn't work ?
PHP Code:
#include <iostream>
#include <windows.h> //Must if you're using LoadLibrary, etc
//Prototype
void LoadDll(void);
//Must declare a function pointer type you want
typedef int (*MYFUNCTION)(int);
void main(void)
{
LoadDll();
int a = 5;
a = (*myfunc)(a);
std::cout << a;
}//End main
void LoadDll(void)
{
HINSTANCE hDLL = LoadLibrary("deuxDll.dll");
if (hDLL)
MYFUNCTION myfunc = (MYFUNCTION)GetProcAddress(hDLL, "doubleInt");
//You can free the DLL here just for performance
//FreeLibrary(hDLL);
else
std::cout << "Error loading DLL";
}//End LoadDll
Error:
Code:
error C2065: 'myfunc' : undeclared identifier
error C2100: illegal indirection
-
Jul 16th, 2002, 01:58 PM
#7
PowerPoster
Declare "myfunc" on the top of your code.
PHP Code:
#include <iostream>
#include <windows.h> //Must if you're using LoadLibrary, etc
//Prototype
void LoadDll(void);
//Must declare a function pointer type you want
typedef int (*MYFUNCTION)(int);
MYFUNCTION myfunc; //Declare it
void main(void)
{
LoadDll();
int a = 5;
a = (*myfunc)(a);
std::cout << a;
}//End main
void LoadDll(void)
{
HINSTANCE hDLL = LoadLibrary("deuxDll.dll");
if (hDLL)
myfunc = (MYFUNCTION)GetProcAddress(hDLL, "doubleInt");
//You can free the DLL here just for performance
//FreeLibrary(hDLL);
else
std::cout << "Error loading DLL";
}//End LoadDll
Last edited by abdul; Jul 16th, 2002 at 03:02 PM.
Baaaaaaaaah
-
Jul 16th, 2002, 02:04 PM
#8
Ok but I have a speacial msgbox with an error when I execute, maybe I have an error when is time to load the DLL in the pointer.
...thx for debugging me I appreciate.
-
Jul 16th, 2002, 02:15 PM
#9
When I pass the program in debug I have RED hex code in the debugger screen... I think I do something wrong
-
Jul 16th, 2002, 02:46 PM
#10
-
Jul 17th, 2002, 02:47 AM
#11
Monday Morning Lunatic
Originally posted by DaoK
When I pass the program in debug I have RED hex code in the debugger screen... I think I do something wrong
It's highlighted in red if the value's changed 
Everything working now?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|