Solved

HOw to create a Win32 Dll ?

Posted on 1998-06-15
11
323 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
  • 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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