Click here to Skip to main content
15,030,240 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

My question is very simple. I have created 2 projects:

1) MyApp - MyApp.exe
2) Mydll - Mydll.dll

Mydll.h
// Mydll.h : main header file for the Mydll DLL
//

#pragma once

#ifndef __AFXWIN_H__
	#error "include 'stdafx.h' before including this file for PCH"
#endif

#include "resource.h"		// main symbols


// MydllApp
// See Mydll.cpp for the implementation of this class
//

class MydllApp : public CWinApp
{
public:
	MydllApp();

// Overrides
public:
	virtual BOOL InitInstance();

	DECLARE_MESSAGE_MAP()
};



MydllDlg.h

#pragma once
// MydllDlg dialog
class MydllDlg : public CDialog
{
    DECLARE_DYNAMIC(MydllDlg)
public:
    MydllDlg(CWnd* pParent = NULL);   // standard constructor
    virtual ~MydllDlg();
// Dialog Data
    enum { IDD = IDD_DIALOG_CRINV };
protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    DECLARE_MESSAGE_MAP()
};


I have created these projects separately.

Now, I want io include this dll to the app so that I can instantiate obj and call methods of the dll from the app.

What are the correct steps I need to do in order to success this.
(Kindly explain in DETAIL the steps as i'm NEW to software development)

If there were already some threats or explaination on this done kindly send me the link.

I have do some searching but, it does NOT HELP me because in those forums the answers were given to ppl who are knowlegeable in MVS IDE. (e.g "reference your dll to the project and import the dll in the project")...seriously I tell you, I don't know how to do that!

Sorry if I'm too harsh :)

Leaner,
skunkhead :)
Posted
Updated 9-Apr-11 20:05pm
v2

You do not "include" DLLs. Another name for them is "Shared Libraries". They are linked to or loaded during run-time. They are shared because other MZ file (Portable Executable) can also link or load the DLL from the same file.

Here you can find instruction of how to reference them:
http://stackoverflow.com/questions/414761/how-do-i-reference-a-dll-in-a-vc-project[^]
http://stackoverflow.com/questions/809948/dll-references-in-visual-c[^]

As to the loading DLLs dynamically, you really need to know two Windows APIs: LoadLibrary (http://msdn.microsoft.com/en-us/library/ms684175(v=vs.85).aspx[^]) and GetProcAddress (http://msdn.microsoft.com/en-us/library/ms683212(v=vs.85).aspx[^]). First function return is used as input for another one.

—SA
   
v2
Comments
Albert Holguin 10-Apr-11 2:45am
   
great references sa, my 5
Sergey Alexandrovich Kryukov 10-Apr-11 19:42pm
   
Thank you, Albert,
--SA
Espen Harlinn 10-Apr-11 9:35am
   
Nice links, 5ed!
Sergey Alexandrovich Kryukov 10-Apr-11 19:42pm
   
Thank you, Espen.
--SA
Take a look at this tutorial: http://www.hdsoft.org/howtolinktodll.html[^]
   
Comments
skunkhead 9-Apr-11 22:56pm
   
thanks Hans,
Your link was a good one! I'm using Implicit linking.

I'm able to compile the linked App. Perfect!

Now, I need to create an object/instance of the dll in the App class.
What i did:
//header
#include mydll.h //Compile Perfectly!
...
void CMyAppDlg::OnBnClickedBtnCrInv()
{
// TODO: Add your control notification handler code here
Mydll test; //Compile error
}

I'm getting error:
error C2065: 'Mydll' : undeclared identifier
syntax error : missing ';' before identifier 'test'
error C2065: 'test' : undeclared identifier

If i'm not mistaken the way i create the instance is wrong.
May I have your advice on this?
Hans Dietrich 10-Apr-11 0:17am
   
If you are just trying to call a function in the DLL, then all you have to say is
myfunct();
with the appropriate parameters, of course. Since you haven't shown any code, or said what was in the DLL, this is my best guess.
skunkhead 10-Apr-11 1:22am
   
is this mean that I no need to create an instance of the dll class?straight calling the dll's function will be fine?

Mydll:
it has MydllDlg.cpp. This will have a dialoguebox with some controls.Not more.
I want the MyAppDlg::OnBnClickedBtnCrInv() to open the dialoguebox of dll.
Hans Dietrich 10-Apr-11 1:47am
   
It sounds like you have a dialog class in your DLL. Is that correct? If it is, then yes, you have to create an instance of your class. "MydllDlg.cpp" is a filename. This tells me nothing. Does it contain an implementation of a dialog class? Please show the contents of mydll.h.
skunkhead 10-Apr-11 2:06am
   
I have update the question with the implementation of the header files. Please have a look.
Hans Dietrich 10-Apr-11 3:53am
   
To shortcut the back-and-forth, I have created a demo that shows how to display a dialog from a DLL, Please download it here:
http://www.hdsoft.org/download/MfcExtDllTest.zip
Please post here if you have questions.
Espen Harlinn 10-Apr-11 6:52am
   
Nice link, 5ed! :)
Sergey Alexandrovich Kryukov 10-Apr-11 19:43pm
   
Agree. My 5.
--SA
My favourite solution is to use:
#ifndef BUILDING_MYDLL
 #pragma comment( lib, "mydll" )
#endif

It's documented here:
http://msdn.microsoft.com/en-us/library/7f0aews7(v=vs.80).aspx[^]

I usually embed the #pragma in the header file, so all that's required is to #include the right header, and the linker has all the information it needs - nice and simple :)

Regards
Espen Harlinn
   
Comments
Hans Dietrich 10-Apr-11 9:16am
   
Yes, I use this too. Since it was a newbie question, I wanted to stick to the basics.
Espen Harlinn 10-Apr-11 9:34am
   
And I rewarded you a 5 too :)
Hans Dietrich 10-Apr-11 11:38am
   
Gave you a 5, one of my favorite tricks.
Espen Harlinn 10-Apr-11 11:49am
   
Thanks Hans :)
Albert Holguin 10-Apr-11 15:25pm
   
once again... elegant solution.. my 5
Espen Harlinn 10-Apr-11 15:45pm
   
Thanks Albert :)
Sergey Alexandrovich Kryukov 10-Apr-11 19:45pm
   
This is very convenient and robust. My 5.
Sorry my ignorance: is is a part of C++ standard?
--SA
Espen Harlinn 11-Apr-11 15:07pm
   
Thanks SAKryukov,
Each implementation of C and C++ supports some features unique to its host machine or operating system. Some programs, for instance, need to exercise precise control over the memory areas where data is placed or to control the way certain functions receive parameters. The #pragma directives offer a way for each compiler to offer machine- and operating system-specific features while retaining overall compatibility with the C and C++ languages. Pragmas are machine- or operating system-specific by definition, and are usually different for every compiler.

Sergey Alexandrovich Kryukov 11-Apr-11 15:14pm
   
Sure. Thank you for the answer.
--SA

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