Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
Hi,
I have written a sample code for exporting functions from a DLL to an EXE. It worked well. And, I decided to post it here for reference.

However, I would like to make this a complete reference. So, I will need to add exporting function from an EXE to a DLL, which I failed to implement correctly.

I need your help in fixing my error. It compiles correctly, however keeps crashing. I am not sure whether it is from the exported variables or from the exported functions.

I will write the parts that I want help with first!

p.s. Man I hate this automated tagger!!!!!

* Environment: Windows, Language: C


-------EXPORTING A FUNCTION FROM THE EXE TO THE DLL. (What am I doing wrong here? Cuz mine doesn't seem to work!!! )
This piece of code is in the EXE's header file.
C#
__declspec(dllexport) RETURNVALUE	MYFUNCTION( parameters )


This piece of code is in the DLL's header file.
C#
__declspec(dllimport) RETURNVALUE     MYFUNCTION( parameters )


Finally I added the "MYEXE.lib" to the additional dependencies YOURDLL project

-------EXPORTING A VARIABLE FROM THE EXE TO THE DLL. (I need help here too)
C#
//structuretype_t is an alias to structuretype_s
// but I don't know why in hell do I need to write the word struct with one and not the other to shut the compile up ><'
__declspec(dllimport) structuretype_t		MYVAR2;///
__declspec(dllimport) struct structuretype_s	*pMYVAR2;//



------------------------Below is working! So please ignore, placed for reference only!
* Callback functions:
C#
//Note: CAPS user-custom-specific namings.
// if caller is EXE to DLL you need to use either __cdecl or __stdcall somewhere, but I'm not sure where and which.
// parameters: is the ones of the function you want to call back to.
// NOTE: if parameters is noting but "void" then leave the brackets empty.
// I used a personal convention such as CB for Callback, use whatever you like instead.
typedef void (* func_FUNCTIONNAME )(parameters); 
void CB_FUNCTIONNAME( func_FUNCTIONNAME pFUNCTIONNAME, parameters );// CB: Callback
//void CB_FUNCTIONNAME( func_FUNCTIONNAME pFUNCTIONNAME );// use this one if the parameters were "void"

void caller_function( void )
{
	func_FUNCTIONNAME pFUNCTIONNAME;
	HMODULE hLib;
	hLib = LoadLibrary(TEXT("YOURDLL.dll"));
// lines of code
	pFUNCTIONNAME = (func_FUNCTIONNAME)GetProcAddress(hLib, TEXT("FUNCTIONNAME"));
	if ( pFUNCTIONNAME == NULL) {
    return;
//Lines of code
	CB_FUNCTIONNAME(pFUNCTIONNAME, parameters);// Calling to the callback function here.
	CB_FUNCTIONNAME(pFUNCTIONNAME);// Use this if there were no parameters.
}

// the definition of the callback function
void CB_FUNCTIONNAME( func_FUNCTIONNAME pFUNCTIONNAME, parameters )
//void CB_FUNCTIONNAME( func_FUNCTIONNAME pFUNCTIONNAME ) // use this one if the parameters are void
{
    pFUNCTIONNAME( parameters );// leave brackets empty if the parameters are void
    return;
}

//finally, the add __declspec(dllexport) at the start the definition of the function you are calling back too. For example:
__declspec(dllexport) void CallBackToMe( void )
{
//do nothing
 return;
}


-------EXPORTING A VARIABLE FROM THE DLL TO THE EXE.
I didn't! I added an additional function to handle editing the variables. I was lucky to have no pointer variables in my program! (^o^)/


Reference1: Calling to an EXE function from inside a DLL[^]
Reference2: I made this callback function what is wrong?[^]
Posted
Updated 23-Feb-11 23:05pm
v2

1 solution

I'm confused: you want us to write a Tip/Trick for you because you can't get it working?

Rather than doing your "tutiorialish" style - which I find confusing - why don't you just write out your question, and explain what your problem is?

Oh! I know! It's because you did that yesterday, were rude to people who tried to help you, and are hoping we didn't notice! Clever!

I made this callback function what is wrong?[^]
 
Share this answer
 
Comments
coffeenet 24-Feb-11 4:44am    
First, I am not the kind of a person who enjoys arguing online, nor offline either. So, I won't give in to you provoking a reaction out of me.
Second, if you read my post yesterday, I fix my original problem that was related to "callback functions", and not to exporting/importing functions using __declspec(dllimport/exporting). Furthermore, if you read my above question here, I have added my fixes in this question, hence, I made no mention to it again.
Third, I do not consider ignoring attempts at picking a fight as "rude".
Forth and last, believe it or not, I wrote the above question in sincerity, I was hoping to make a clear tutorial, because I found so much difficulty understanding syntax conventions related to export/importing functions and variables. And, there are not good enough tutorials out there.

Now to talking about my problem above, yes, I was hoping someone would improve the code above while answering my errors.
I will Improve my question since you say it is confusing. I will simply write it in form of a question.

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