Solved

how i create a DLL, correctly?

Posted on 2002-04-01
10
383 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 how to clear a vector as well as how to detect empty vectors in C++.
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.

863 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

22 Experts available now in Live!

Get 1:1 Help Now