Solved

how i create a DLL, correctly?

Posted on 2002-04-01
10
389 Views
Last Modified: 2012-05-04
I want create a DLL, but i read a MSDN library and our examples don't run correctly.

I want "RUN :  RUNDLL32 MYDLL,Myfunction".


is Example wrong?

// DLL.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
__declspec(dllexport) BOOL WINAPI MyFunction (HWND hwnd, LPLONG lpIValue, LPSTR lpszValue)
{
     MessageBox(NULL,"Message 1 - Test my DLL. Myfunction","Test",MB_OK);
     return 0;
}

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved )
{
    MessageBox(NULL,"Message 2 - Test my DLL, dllmain.","Test",MB_OK);
     return TRUE;
}
------------------------------------------------

When i run this example, and RUN: "RUNDLL32 mydll.dll,myfunction", it show "Message 2", but don't show "Message 1 ". It show Error in DLL.dll "Missing entry:Myfunction".


0
Comment
Question by:alexBrasil
10 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6910843
At first glance your problem is simple.  In the RUNDLL32 call you specify:

"Myfunction"

In the function you specify:

"MyFunction"

DLL calls ARE CASE SENSITIVE!!
0
 
LVL 86

Expert Comment

by:jkr
ID: 6910889
make it read

// DLL.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"

extern "C" // <--!!
__declspec(dllexport) BOOL WINAPI MyFunction (HWND hwnd, LPLONG lpIValue, LPSTR lpszValue)
{
    MessageBox(NULL,"Message 1 - Test my DLL. Myfunction","Test",MB_OK);
    return 0;
}

BOOL APIENTRY DllMain( HANDLE hModule,
                      DWORD  ul_reason_for_call,
                      LPVOID lpReserved )
{
   MessageBox(NULL,"Message 2 - Test my DLL, dllmain.","Test",MB_OK);
    return TRUE;
}

And it should work - I assume you have a .cpp file, so this is necessary to turn off the C++ name mangling...
0
 
LVL 86

Expert Comment

by:jkr
ID: 6910891
Oh, and jhance is of course right, the names are case sensitive also...
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 2

Expert Comment

by:venkaiah
ID: 6912416
Try in this way

#include "stdafx.h"
extern "C" __declspec(dllexport) BOOL MyFunction (HWND hwnd, LPLONG lpIValue, LPSTR lpszValue)
{
    MessageBox(NULL,"Message 1 - Test my DLL. Myfunction","Test",MB_OK);
    return 0;
}

BOOL APIENTRY DllMain( HANDLE hModule,
                      DWORD  ul_reason_for_call,
                      LPVOID lpReserved )
{
   MessageBox(NULL,"Message 2 - Test my DLL, dllmain.","Test",MB_OK);
    return TRUE;
}
i think this will helps you.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6912875
venkaiah,

this already has been covered...
0
 
LVL 1

Accepted Solution

by:
jemax earned 70 total points
ID: 6914250
Hi,

sorry, guys, but as far as I know, using "__declspec(dllexport)" leaves func. name decoration on compiler own, so the exported name (visible from outside (using "Depends" utility from VS)) will be something like "_MyFunction@12". If you call rundll32 mydll.dll _MyFunction@12 you will see both messages. If you want to decorate the name by yourself, use .DEF file.


alexBrasil, run "Depends" from VS tools, open your dll, and check the actual name of your exported function.

Best regards,
jemax
0
 
LVL 32

Expert Comment

by:jhance
ID: 6914262
Am I the only one who can see that this question has already been answer in full?
0
 
LVL 86

Expert Comment

by:jkr
ID: 6914441
jhance: No, you are not :o)

Without a case-correct spelling, the rundll call won't work, but neither would it with a C++ mangled name as the export...
0
 
LVL 32

Expert Comment

by:jhance
ID: 6914449
It sure would be nice if alexBrasil would come back and attend to his question.....
0
 
LVL 86

Expert Comment

by:jkr
ID: 6914454
Well, this really would be helpful, indeed...
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

776 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