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

x
?
Solved

exports function from .DLL

Posted on 1999-07-30
18
Medium Priority
?
729 Views
Last Modified: 2008-02-20
hi,every expert:
I want to export functions from a .dll and I'm using VC++.
I don't wanna using the .DEF but delspec(dllexport) instead and I will use Loadlibrary() and loadprocaddress() to use the  function in .DLL.
I don't know how to use delspec(dllexport) to export a function from a .DLL.But I know that can do the job.
Can some expert give me a small example?
Thanks...
0
Comment
Question by:geeboy
[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
18 Comments
 
LVL 6

Accepted Solution

by:
MichaelS earned 160 total points
ID: 1201478
I guess you are about GetProcAddress()?

extern "C" void __declspec(dllexport) YourFunction(void);

extern "C" void __declspec(dllexport) YourFunction(void)
{
}


0
 

Author Comment

by:geeboy
ID: 1201479
Yes,GetProcAddress().
I try to use that you said to do this,but I fail.If I use a .DEF,I can do it.
I recompile my .dll when I add  extern "C" __declspec(dllexport) without changing the .exe code(that can ok using .def).

Then I load the .dll successfully and when I
GerProcAddress(), It returns NULL, the code number is 126 meaning:The specified module could not be found.

What's the problem?

Here is my function in .dll:
extern "C" LRESULT CALLBACK  __declspec(dllexport) MyProc(int nCode, WPARAM  wParam,  LPARAM  lParam ) ;

extern "C" LRESULT CALLBACK __declspec(dllexport) MyProc(int nCode, WPARAM  wParam,LPARAM  lParam )
{do somethings
}
 
In the .exe:

hinstDLL = LoadLibrary((LPCTSTR) "xxx.dll");//it's ok.  
hprc = (HOOKPROC)GetProcAddress(hinstDLL, "MyProc");


Can you find something is wrong or missed?
Thanks

0
 
LVL 3

Expert Comment

by:PinTail
ID: 1201480
run DUMPBIN /EXPORTS xxx.dll
look for MyProc, if you can't see it, then you have done somehting wrong
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Expert Comment

by:MichaelS
ID: 1201481
You can also have a lot at your dll by quickview.
0
 

Author Comment

by:geeboy
ID: 1201482
I have finded using dumpbin that the MyProc became
"_MyProc@12" in .DLL ..
Why?
I must use "_MyProc@12" in the GetProcAddress() ?
I don't think so,why cant I just use MyProc as when I use a .DEF?
BTW: I have used the extern "C"...

0
 
LVL 6

Expert Comment

by:MichaelS
ID: 1201483
May be you have to delete all obj files and rebuild all project or something like that. And be sure you don't link your library staticaly.
0
 
LVL 6

Expert Comment

by:MichaelS
ID: 1201484
May be you have to delete all obj files and rebuild all project or something like that. And be sure you don't link your library staticaly.
0
 

Author Comment

by:geeboy
ID: 1201485
Ohhh,I'm in despair:
I do everything,even I build a new project and add the .dll into it and build it ,It still like this:
"_MyProc@12"

Here is my code in the .dll:
#include "windows.h"
extern "C"  __declspec(dllexport) LRESULT CALLBACK  MyProc(int nCode, WPARAM  wParam,LPARAM  lParam    ) ;

LRESULT CALLBACK  MyProc(int nCode, WPARAM  wParam,  
                                LPARAM  lParam  
                            )
{something here;
                  

else return 0;
}
 
Something wrong?
Why it's "_MyProc@12" not "MyProc"?


0
 
LVL 6

Expert Comment

by:MichaelS
ID: 1201486
Where is
extern "C"?
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 1201487
See
http://www.borland.com/devsupport/borlandcpp/ti_list/TI3242.html
about function names in VC and what make with it(use DEF!).

0
 

Author Comment

by:geeboy
ID: 1201488
To MichaelS:
You can't see the "extern "C""?
It's in the declaration of that function...

0
 

Author Comment

by:geeboy
ID: 1201489
To AlexVirochovsky:
There is no content there.
0
 
LVL 6

Expert Comment

by:MichaelS
ID: 1201490
Yes, in the declaration, but there is no extern "C" in the defenition.
0
 

Author Comment

by:geeboy
ID: 1201491
It's not necessary to add to the definition ,however I add but It still seems like:
"_MyProc@12"

I eliminate the "extern "C"" and It seems like:
"?MyProc@@yghizj@z"
This is the decorated C++ function.

Maybe if we use __declspec(dllexport) to export functions from .dll ,we can't get the non-decorated function name
that We can using .def..

Out of my curiosity, MichaelS, have you ever use __declspec(dllexport) and succeed in explicitly loading  function from .dll by GetProcAddress()?

Here is my full code:

include "windows.h"
extern "C"  __declspec(dllexport) LRESULT CALLBACK  Proc(int nCode, WPARAM  wParam,  
                                LPARAM  lParam  
                            ) ;

extern "C"  __declspec(dllexport) LRESULT CALLBACK  Proc(int nCode, WPARAM  wParam,  
                                LPARAM  lParam  
                            )
{ if ( nCode>=0)
        return 1;
                  

else return 0;
}

You can compile it and try using the dumpbin...

Thanks ...
0
 
LVL 6

Expert Comment

by:MichaelS
ID: 1201492
Everything is OK if you remove CALLBACK.
0
 

Author Comment

by:geeboy
ID: 1201493
Yes, I remove the CALLBACK and it's ok.
However, it must be a CALLBACK function 'cause it's a hook function.
Is this a bug of the VC?
0
 
LVL 6

Expert Comment

by:MichaelS
ID: 1201494
I guess you can't use CALLBACK with C. You have two options (as for me):

-use complex function name which C++ generated;
-call your hook function from the dll.

At my hool dll I created function SetHook() in which I sets a hook.
0
 

Author Comment

by:geeboy
ID: 1201495
Thank you!!!!!!!!!!!!!!1
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

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…
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…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

715 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