Click here to Skip to main content
15,898,588 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralStack overflow Pin
LiYS25-Jul-05 0:33
LiYS25-Jul-05 0:33 
GeneralRe: Stack overflow Pin
mark novak25-Jul-05 0:47
mark novak25-Jul-05 0:47 
GeneralRe: Stack overflow Pin
LiYS25-Jul-05 0:49
LiYS25-Jul-05 0:49 
GeneralRe: Stack overflow Pin
khan++25-Jul-05 0:54
khan++25-Jul-05 0:54 
GeneralRe: Stack overflow Pin
LiYS25-Jul-05 3:47
LiYS25-Jul-05 3:47 
GeneralRe: Stack overflow Pin
toxcct25-Jul-05 5:47
toxcct25-Jul-05 5:47 
GeneralRe: Stack overflow Pin
Tim Smith26-Jul-05 3:52
Tim Smith26-Jul-05 3:52 
GeneralThe old time classic dll questions! Pin
Themis24-Jul-05 23:58
Themis24-Jul-05 23:58 
Hi, as you 've probably guessed I tring to create a dll and use it in another program. So I've search some articles and I 've found a few interesting things. However when I try to execute a simple program it doesn't seem to work. Let's be a bit more specific.

<br />
#include "stdafx.h"<br />
#include "greet.h"<br />
#include <cstdio><br />
<br />
BOOL APIENTRY DllMain( HANDLE hModule, <br />
                       DWORD  ul_reason_for_call, <br />
                       LPVOID lpReserved<br />
					 )<br />
{<br />
	switch (ul_reason_for_call)<br />
	{<br />
	case DLL_PROCESS_ATTACH:<br />
	case DLL_THREAD_ATTACH:<br />
	case DLL_THREAD_DETACH:<br />
	case DLL_PROCESS_DETACH:<br />
		break;<br />
	}<br />
    return TRUE;<br />
}<br />
<br />
// This is an example of an exported variable<br />
GREET_API int ngreet=0;<br />
<br />
// This is an example of an exported function.<br />
GREET_API int fngreet(void)<br />
{<br />
	return 42;<br />
}<br />
<br />
// This is the constructor of a class that has been exported.<br />
// see greet.h for the class definition<br />
Cgreet::Cgreet()<br />
{ <br />
	return; <br />
}<br />
<br />
void Cgreet::run(int arg)<br />
{<br />
	printf("Hello World!\n");<br />
}<br />


The above code is produced by VS 7.1 when asked to create a new win32 console project and dll is selected. I 've also added another method to the class created. I also give you the header file VS created.

<br />
#ifdef GREET_EXPORTS<br />
#define GREET_API __declspec(dllexport)<br />
#else<br />
#define GREET_API __declspec(dllimport)<br />
#endif<br />
<br />
// This class is exported from the greet.dll<br />
class GREET_API Cgreet {<br />
public:<br />
	Cgreet(void);<br />
	// TODO: add your methods here.<br />
<br />
	void run(int arg);<br />
};<br />
<br />
extern GREET_API int ngreet;<br />
<br />
GREET_API int fngreet(void);<br />


Well now I create a simple win32 console project with the following code
<br />
#include <cstdio><br />
#include <windows.h><br />
<br />
int main()<br />
{<br />
	HMODULE hdll = LoadLibrary("greet.dll");<br />
	if (hdll != NULL)<br />
	{<br />
		typedef int (*PFUNC)(void);<br />
<br />
		PFUNC greet = (PFUNC)GetProcAddress(hdll, "fngreet");<br />
		if (greet != NULL) {<br />
			// Now we can call the function in the DLL<br />
			printf("greet.dll returned: %d!\n", greet());<br />
		}<br />
		else<br />
			printf("Loading function 'fngreet' failed!\n");<br />
	}<br />
	else<br />
		printf("Loading library 'greet.dll' failed!\n");<br />
}<br />


The output of execution is:
'Loading function 'fngreet' failed!\n'

I 've also seen in another post someone asked functions like "_fngreet" but that also didnt work. What am I doing wrong? Looking the code with the debugger I see in my main program that hdll pointer gets a weird address.
hdll 0x10000000 {unused=9460301 } HINSTANCE__ *
That's what the debugger says about hdll and of course greet fpointer is null.

Another question, let's say that I manage to get the function pointer loaded dynamicly. How will I load the Cgreet class? Does GetProcAddress works also for classes? What about variables?

That's all I guess!
Cheers, Themis
GeneralRe: The old time classic dll questions! Pin
mark novak25-Jul-05 0:41
mark novak25-Jul-05 0:41 
QuestionAbout ComboBox Owner draw problem? Pin
starschen24-Jul-05 23:47
sussstarschen24-Jul-05 23:47 
AnswerRe: About ComboBox Owner draw problem? Pin
mark novak25-Jul-05 0:24
mark novak25-Jul-05 0:24 
GeneralRe: About ComboBox Owner draw problem? Pin
starschen25-Jul-05 15:28
sussstarschen25-Jul-05 15:28 
GeneralRe: About ComboBox Owner draw problem? Pin
mark novak25-Jul-05 15:40
mark novak25-Jul-05 15:40 
GeneralRe: About ComboBox Owner draw problem? Pin
starschen25-Jul-05 17:24
sussstarschen25-Jul-05 17:24 
GeneralRe: About ComboBox Owner draw problem? Pin
mark novak25-Jul-05 17:30
mark novak25-Jul-05 17:30 
GeneralRe: About ComboBox Owner draw problem? Pin
starschen25-Jul-05 17:42
sussstarschen25-Jul-05 17:42 
GeneralRe: About ComboBox Owner draw problem? Pin
mark novak25-Jul-05 18:16
mark novak25-Jul-05 18:16 
GeneralRe: About ComboBox Owner draw problem? Pin
starschen25-Jul-05 19:29
sussstarschen25-Jul-05 19:29 
GeneralToolbar Repainting problem Pin
Project Leader24-Jul-05 23:45
Project Leader24-Jul-05 23:45 
GeneralRe: Toolbar Repainting problem Pin
Jose Lamas Rios25-Jul-05 16:56
Jose Lamas Rios25-Jul-05 16:56 
GeneralOutlook Problem - 'Person Name Smart Tag' custom functionality Pin
Rajeev K Srivastava24-Jul-05 23:41
Rajeev K Srivastava24-Jul-05 23:41 
QuestionHow can i put a Link / Hyperlink into a MessageBox? Pin
IronMike24-Jul-05 23:10
IronMike24-Jul-05 23:10 
AnswerRe: How can i put a Link / Hyperlink into a MessageBox? Pin
khan++24-Jul-05 23:25
khan++24-Jul-05 23:25 
AnswerRe: How can i put a Link / Hyperlink into a MessageBox? Pin
David Crow25-Jul-05 3:06
David Crow25-Jul-05 3:06 
GeneralRe: How can i put a Link / Hyperlink into a MessageBox? Pin
James R. Twine25-Jul-05 4:35
James R. Twine25-Jul-05 4:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.