Mourning the loss of our friend, WhitPhil.
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Software Development
Tag Cloud
access audio black screen blue screen bsod connection crash desktop drivers dvd email error excel excel 2003 firefox google hard drive hardware hdmi hijackthis internet itunes keyboard laptop malware monitor motherboard network networking outlook problem ram recovery router screen slow sound spyware tdlwsp.dll trojan vba video virus vista vundo windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
How To Create A .Dll in Visual C++ Express 2008

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

Closed Thread
 
Thread Tools
-Fabez-'s Avatar
Senior Member with 1,499 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: Advanced - Einstein
07-Dec-2008, 03:11 PM #1
How To Create A .Dll in Visual C++ Express 2008
Is it possible to create .Dll's in Visual C++ Express 2008 and if so how ? Also any help on how to code a .Dll would be much appreciated. Thanks -Fabez-
mt2002's Avatar
Computer Specs
Senior Member with 878 posts.
 
Join Date: Sep 2005
Location: 127.0.0.1
Experience: Advanced
07-Dec-2008, 05:36 PM #2
Of course

Project Properties->Configuration Properties->General (Usually automatically opens as soon as you open project properties)

Then change configuration type to Dynamic Library (*.dll). Rebuild and the module will be output as a dll.

Working with *.dll's are a little different though. They do not require an entry point (If there is one, it should be DLLMain()). And any symbolic information that you want to export from the dll (perhaps to provide the dll an interface) requires __declspec (dllexport) and be imported by the program using the dll (__declspec (dllimport)). I can post an example if needed.
__________________

"Within C++, there is a much smaller and cleaner language struggling to get out." - Bjarne Stroustrup
Work: Tech Support, Programmer; A+ 220-602 Certified
[Website] [Neptune]
-Fabez-'s Avatar
Senior Member with 1,499 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: Advanced - Einstein
08-Dec-2008, 04:22 PM #3
Thanks for the information An example would be greatly appreciated, preferably a minimal example that works with vitually all compilers like Visual C++ and Dev C++ for example. Thanks -Fabez-
mt2002's Avatar
Computer Specs
Senior Member with 878 posts.
 
Join Date: Sep 2005
Location: 127.0.0.1
Experience: Advanced
08-Dec-2008, 05:03 PM #4
I cant show an example that will work on all compilers as there is no portable way. Dll linkage is not defined by the standard thus there is no possible way without using compiler extensions.

I have only done it using Visual C++ 6, 2005, and 2008 thus can only post an example in that compiler.
__________________

"Within C++, there is a much smaller and cleaner language struggling to get out." - Bjarne Stroustrup
Work: Tech Support, Programmer; A+ 220-602 Certified
[Website] [Neptune]
-Fabez-'s Avatar
Senior Member with 1,499 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: Advanced - Einstein
09-Dec-2008, 12:01 PM #5
Okay then, can you post a Visual C++ example please. Thanks -Fabez-
mt2002's Avatar
Computer Specs
Senior Member with 878 posts.
 
Join Date: Sep 2005
Location: 127.0.0.1
Experience: Advanced
09-Dec-2008, 07:42 PM #6
Creating the DLL

Create a new project and a new source and header file for it. Call them dllheader.h and main.cpp for now. Go to your project properties and set it to a DLL project (See my previous post).

This is a small tutorial-post that will show you how to create the DLL and use it in another program. Keep in mind, however, that it does not show you how to dynamically load and work with DLLs. For that, you need to look into LoadModule or LoadLibrary routines. Its easy though and will NOT require needing an import library file that this tutorial has. That is, this tutorial-post requires you to set an extra dependency that is statically linked into the project and delay-loads the DLL for simplicity. I can show you how to modify this demo after you get it working to dynamically work with the DLL if you like.

Anyways, on with the show

Add the following code to dllheader.h:

Code:
#ifndef DLLHEADER_H_INCLUDED
#define DLLHEADER_H_INCLUDED

#ifdef DLL_EXPORT
# define EXPORT __declspec (dllexport)
#else
# define EXPORT
#endif

extern EXPORT void foo ();

#endif
foo() is the routine that the DLL will export. However we only want to export it when building the DLL project. Because of this, only the DLL project should define DLL_EXPORT.

Now for main.cpp in the DLL's project...

Code:
#define DLL_EXPORT
#include "dllheader.h"
#include <iostream>

EXPORT void foo () {

	std::cout << "This is the DLL!" << std::endl;
}
All this does is export the routine (by defining DLL_EXPORT). The routine simply prints a screen to let you know it is executing.

Thats all. Build the project and it will output two files: A dll and a lib file. The lib file is for import symbols only and still requires the dll to run if you use it. Using the lib file alone will cause an error from Windows that the dll file is missing.

__declspec is MSVC specific.

Test the dll

Create a new project and create a new source file. Set up the header include paths/library paths (or just copy the lib, dll, and dllheader.h file to the new projects working directory).

Also in this project settings go to Configuation properties->Linker->Input and then look for Delay Loaded DLL. Enter the path to the DLL you have just created. Also, in Additional Dependencies give it the path to the import library (*.lib) file that was created.

Test it like this (Assumes you set the include directory paths to where dllheader is located at) :

Code:
#include <dllheader.h>

int main () {

	foo ();
}
Build the project and test it. If it works, the string printed from the DLL should display.
__________________

"Within C++, there is a much smaller and cleaner language struggling to get out." - Bjarne Stroustrup
Work: Tech Support, Programmer; A+ 220-602 Certified
[Website] [Neptune]
Cookiegal's Avatar
Administrator with 63,607 posts.
 
Join Date: Aug 2003
Location: Quebec, Canada
07-Apr-2009, 03:01 PM #7
Reopening thread, as requested.
-Fabez-'s Avatar
Senior Member with 1,499 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: Advanced - Einstein
08-Apr-2009, 07:42 AM #8
Thanks, Cookiegal After taking the above example and working through it, I have moved onto dynamically loading .Dll's into my C and C++ code. I have been using the following code to export functions from my .Dll.

Code:
extern "C" __declspec(dllexport) void HelloWorld (){
        cout << "Hello World: Dll Style !!";
    }
This approach has been largely successful, eliminating the need for a .Def file as well as no name mangling, however when I attempt to to export the following function, its name becomes mangled, despite the use of the extern command.

Code:
extern "C" __declspec(dllexport) LRESULT CALLBACK  KeyboardProc(int code,
    WPARAM wParam,
    LPARAM lParam){
}
Is there anything I can do to prevent the mangling of the functions name ?
__________________
Like coding, want help with coding or want to learn coding ? Then the Coders Group is for you
mt2002's Avatar
Computer Specs
Senior Member with 878 posts.
 
Join Date: Sep 2005
Location: 127.0.0.1
Experience: Advanced
08-Apr-2009, 06:17 PM #9
CALLBACK is _stdcall, which will mangle your function names. You will need to either get rid of CALLBACK in your code (defaults to _cdecl) or replace CALLBACK with _cdecl.

i.e., removing CALLBACK should remove the name mangling. Of course, if this routine is called by Windows directly (as a Windows callback method) then there is no easy way without causing more problems.
__________________

"Within C++, there is a much smaller and cleaner language struggling to get out." - Bjarne Stroustrup
Work: Tech Support, Programmer; A+ 220-602 Certified
[Website] [Neptune]
-Fabez-'s Avatar
Senior Member with 1,499 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: Advanced - Einstein
12-Apr-2009, 11:57 AM #10
If there is no way of preventing the name mangling, I guess I can live with using KeyboardProc@12 After creating my simple hello world .Dll application, I decided to research what else .Dll's could be used for. Msdn indicated that they could be used in global hooks, so I decided to create a program that would take a screen shot of the users screen, when a certain key combination was pressed. However the documentation is unclear, and does not specify which functions have to be placed in the .Dll and which functions have to be placed in the main program. If you could explain which functions go where I would be grateful. I also have a couple of other questions for you

1. What is the point in name mangling ?

2. I asked how to create a .Dll as I wanted to produce modular code as my applications got bigger, to keep my code more organized. When placing your code in .Dll's is it possible to declare something in one and use it in another, perhaps using extern ?
__________________
Like coding, want help with coding or want to learn coding ? Then the Coders Group is for you
mt2002's Avatar
Computer Specs
Senior Member with 878 posts.
 
Join Date: Sep 2005
Location: 127.0.0.1
Experience: Advanced
12-Apr-2009, 03:47 PM #11
Quote:
1. What is the point in name mangling ?
Mangling symbolic names in the global namespace to reduce the possibility of the same symbolic name being defined twice (A common linker error comes to mind...)

Quote:
2. I asked how to create a .Dll as I wanted to produce modular code as my applications got bigger, to keep my code more organized. When placing your code in .Dll's is it possible to declare something in one and use it in another, perhaps using extern ?
Yes it is possible in the same way that you described

The functions can go anywhere, it really does not matter. A DLL is just a standard program with a different extension after all-no different from a regular EXE program.
__________________

"Within C++, there is a much smaller and cleaner language struggling to get out." - Bjarne Stroustrup
Work: Tech Support, Programmer; A+ 220-602 Certified
[Website] [Neptune]
IMM's Avatar
IMM IMM is offline IMM is authorized to help remove malware.
Distinguished Member with 3,232 posts.
 
Join Date: Feb 2002
12-Apr-2009, 11:18 PM #12
Have you tried clueing the linker in about it, using
#pragma comment( linker, "/export:_TheFunc" )
and declaring the func extern as you are doing?

http://msdn.microsoft.com/en-us/libr...k5(VS.71).aspx
http://www.microsoft.com/msj/archive/S202B.aspx

Hooks and callbacks -- sigh
I like .def files
-Fabez-'s Avatar
Senior Member with 1,499 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: Advanced - Einstein
13-Apr-2009, 09:33 AM #13
Mt2002 and IMM, thanks for the information, I will try and alert the linker to the functions I would like to export. But first, I've got to get the program working All of the functions related to the global hook are in the .Dll and the .Exe calls the main function, which should start the hook. However, no error messages are generated and the hook does not appear to function, as no output file is generated. Is there anything I might have forgotten to add to make it work ?
__________________
Like coding, want help with coding or want to learn coding ? Then the Coders Group is for you
mt2002's Avatar
Computer Specs
Senior Member with 878 posts.
 
Join Date: Sep 2005
Location: 127.0.0.1
Experience: Advanced
14-Apr-2009, 06:44 PM #14
Im not sure by what it is that you meen by a "global hook", sorry. Assuming I understand your previous post, your main program calls a function from your DLL to "start the hook". I am also having to assume that the function in the DLL is being called properly.

If that is the case, please elaborate what it is that you are trying to do and I may be able to better assist you
__________________

"Within C++, there is a much smaller and cleaner language struggling to get out." - Bjarne Stroustrup
Work: Tech Support, Programmer; A+ 220-602 Certified
[Website] [Neptune]
IMM's Avatar
IMM IMM is offline IMM is authorized to help remove malware.
Distinguished Member with 3,232 posts.
 
Join Date: Feb 2002
14-Apr-2009, 07:43 PM #15
Quote:
Originally Posted by mt2002 View Post
Im not sure by what it is that you meen by a "global hook"
...
please elaborate what it is that you are trying to do and I may be able to better assist you
I'm with mt2002 on this one -- I'm also in the dark as to what kind of hook this is to be.
Some code from you?
I was guessing along some of these lines
http://www.codeguru.com/cpp/w-p/syst...icle.php/c5667
Are you using Microsoft's SetWindowsHookEx ?

Last edited by IMM : 14-Apr-2009 09:39 PM.
Closed Thread Bookmark and Share

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 01:47 AM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.