Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my exported function in dll1


C++
extern"C" __declspec(dllexport) int FUN1(char* p){
return p[0];
}


I am calling this FUN1 from other project.Below is the code

C++
#include<windows.h>
#include<iostream>
using namespace std;
typedef int (*MYFUN1)(char*);

int main()
{

	HMODULE hMod = LoadLibrary ("C:\\Users\\admin\\Documents\\Visual Studio 2010\\Projects\\CalledFun\\Debug\\CalledFun.exe");
	if(hMod != NULL)
	{
		MYFUN1 pCtor = (MYFUN1) GetProcAddress (hMod, "FUN1");
		int a = pCtor("calling a value") ;
		cout<<a;
	}
}


If I remove the extern "C" from dll1 then that function address is returning as NULL when calling GetProcAddress in dll2.Since both are written in c++ i thought name mangling will not effect this.I thought if we use c and c++ libraries combinely then only we need to mention extern "C" ,Can anyone help me to get out of this confusion

Thanks in advance
Posted
Updated 22-Jul-15 20:43pm
v2
Comments
Sergey Alexandrovich Kryukov 22-Jul-15 23:10pm    
How "will not effect"? A name is a name; mangled or not, it should match. If you use mangled name (why un-mangling it, anyway?) your name in the module using it should match.

Where is your dllimport part? Do you use the same compiler?

—SA
CPallini 23-Jul-15 2:44am    
Virtual 5.

Sergey already explained all that is required to export/import functions.
To punctually answer your question please consider that you are using GetProcAddress(). This function search, in the executable (exe or dll), for a function having exactly the specified name. It doesn't cares to add or remove mangles.
So if you remove "extern C" simply replace, in GetProcaddress(), the mangled name and it will work ;).
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 23-Jul-15 12:44pm    
5ed. I though about mentioning GetProcAddress, where, of course, the developer should use exact name as it is represented in DLL exports, but I forgot that the inquirer actually tried to use GetProcAddress. I'll edit my answer to explain that and add the credit to yours.
—SA
Frankie-C 23-Jul-15 13:09pm    
thanks
Albert Holguin 23-Jul-15 20:08pm    
+5... extern "C" simply makes it easier... unless you speak in mangled terms. The other issue with mangling is that it can also changes based on compiler settings and to add insult to injury, it's non-standard.... so different compilers can make different mangled names.
Frankie-C 24-Jul-15 3:28am    
Thanks Albert.
You're absolutely right, the MS mangling method is almost a de-facto standard, but not a standard anyway. Other compilers can implement different mangling, and in any case it is never a good practice to implement mangled names in exported symbols.
Albert Holguin 24-Jul-15 10:07am    
It's the defacto standard in Windows... not in any other system. I'm still sort of surprised this was never standardized.
Please see: https://msdn.microsoft.com/en-us/library/Aa271769%28v=VS.60%29.aspx[^].

It says that using __declspec(dllimport) and __declspec(dllexport) with the same version of the same compiler produces matching naming on the same function declaration.

If, by some reason, you need to apply arbitrary naming for exported functions or you have to import some functions with arbitrary naming, this problem is solved using DEF files. Please see:
https://msdn.microsoft.com/en-us/library/hyx1zcd3.aspx[^].

As you can see, you can specify both names: the function name as it is seen in the C++ function declaration, and the alternative name (mangled/decorated or not) as it appears in the DLL. See also:
https://msdn.microsoft.com/en-us/library/28d6s79h.aspx[^],
https://msdn.microsoft.com/en-us/library/54xsd65y.aspx[^],
https://msdn.microsoft.com/en-us/library/9h658af8.aspx[^].

[EDIT]

Sorry, I wanted to mention the use of GetProcAddress, but forgot, and forgot that this was what you actually tried to use. Of course, you need to use mangled/decorated name with it. My credit to Solution 2 by Frankie-C.

—SA
 
Share this answer
 
v3
Comments
CPallini 23-Jul-15 2:44am    
Actual 5.
Mohibur Rashid 23-Jul-15 3:41am    
404: 5 not found.
Sergey Alexandrovich Kryukov 23-Jul-15 12:41pm    
:-) :-) !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900