Solved

HOw to create a Win32 Dll ?

Posted on 1998-06-15
11
325 Views
Last Modified: 2010-04-01
I would like to create a Win32 Dll in C++ and test it after but I have no clue how !  
I'm using Visual C++ version 5.0.
Could someone show me how please ! A small sample would be nice !
0
Comment
Question by:tam031198
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1165930
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165931
"File" menu->"New" option->"Projects" tab
Highlight "Win32  Dynamic Link Library" on the list on the left.
Enter a Project name on the right.  I used "TstDll"
Enter location if desired.  Otherwise use default.
Select OK

"File" menu->"New" option->"Files" tab
Highlight "C++ Source File" on the list on the left.
Enter a file name on the right.  I used "TstDll.cpp"

continues.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165932
Past the following code in the source code.
************************************************
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define DllImp __declspec(dllimport)
#define DllExp __declspec(dllexport)

DllExp int ExportedIncrement(int i)
{
   return i + 1;
}

BOOL WINAPI DllMain(HINSTANCE DLLHnd,ULONG Rsn,LPVOID Rsv)
{
   switch (Rsn)
   {
   case DLL_PROCESS_ATTACH:
      break;
   case DLL_PROCESS_DETACH:
      break;
   case DLL_THREAD_ATTACH:
      break;
   case DLL_THREAD_DETACH:
      break;
   }
   return true;
}
**********************************************
The ExportedIncrement is a sample exported  function to get you started.  The DllMain() function is the DLL's initialization/cleanup procedure  (It is documented under that name if you want to learn about it.)

That should get you started.  Questions?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tam031198
ID: 1165933
Thanks Nietod. Howerver when I tried to call the DLL with a program in VB, it gives me an error 49 "BAD DLL CALLING CONVENTION". What does it mean and what should I do ?
0
 

Author Comment

by:tam031198
ID: 1165934
Nietod, I forgot to tell you that if I don't pass any argument to the DLL function, it works fine.  As far as I pass an arguement (an integer e.g.), it gives me that error !
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165935
Sorry, I can't help you there.  This problem is on the VB side--and I don't know anything about VB.  (It would have been a good idea to mention that the DLL would be called by VB in the question.)

Perhaps you can ask on the VB site, or ask a new question here but make clear you have a C++ DLL whose functions need to be called from VB.

If the VB people suggest changes that need to be made on the C++ side, let me know.  I can probably help you with those.  But I think most or all of the work will need to be done on the VB side, when you declare the function in the VB source.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1165936
Opps, I was thinking this answer was already accepted.  (I thought this was an OLD OLD question.)  If you reject the answer someone else might be able to help you.
0
 

Author Comment

by:tam031198
ID: 1165937
Thanks Nietod. Howerver when I tried to call the DLL with a program in VB, it gives me an error 49 "BAD DLL CALLING CONVENTION". What does it mean and what should I do ?
0
 

Author Comment

by:tam031198
ID: 1165938
Nietod, could you tell please me how to call the DLL from Visual C .  Thanks.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165939
As I said I can't help you with the VB side.  What is happening is that VB doesn't like the way you've indicated that the procedure needs to be called or parameters need to be passed.  I can't help you there.  Most likely you need to change the way you declared the procedure in VB, but you also might need to make 1 or 2 small changes from VC.

Calling from VC I can help you with.

In a program that uses the DLL, you just need to
put in a line like

DllExp int ExportedIncrement(int i);

(That is a forward/external declaration of the procedure you want to call.  Look back to the sample code I posted.  This is the declaration for the exported procedure there.)

Then you would use ExportedIncrement like any other finction.

main()
{

int x = 1;
int y = ExportedIncrement(x);  // y = 2;

}

to make this link, you need to link the program with the .lib file from the DLL.  There are two ways to do this.

First you can add the .lib to the project for the exe that uses the DLL.  To do this go to the
"Project" menu =>"Add To Project" menu item => "Files..." menu item.  Then go to the debug or release directory of the DLL project and select the .lib file.  

The other way to get this two link is to make the DLL a subproject of the EXE project that uses the DLL.  To do this you create the EXE project (if you don't have it already.)  Then create the DLL project as I instructed above, but on the dialog where you enter the project name, indicate that it is a subproject (there is a checkbox for this).
0
 

Author Comment

by:tam031198
ID: 1165940
Thank you very much Nietod, I can read my Dll with VC now !
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question