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
Solved

HOw to create a Win32 Dll ?

Posted on 1998-06-15
11
324 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
C++ to C# code conversion issue 4 112
convert char array to number in c 5 86
Safe conversion? 4 72
Embarcadero C++ builder XE10.1 Berlin TRegistry declaration 1 39
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

860 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