Solved

HOw to create a Win32 Dll ?

Posted on 1998-06-15
11
321 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MFC Model Dialogs 10 57
why "." vs "->" 23 115
How to convert MFC APP to Win32 APP. 19 55
Create a path if not exists 7 44
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now