Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

HOw to create a Win32 Dll ?

Posted on 1998-06-15
11
Medium Priority
?
329 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 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

722 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