Solved

(VC++6)how to call two functions having the same name and prototype but different in code each one is in a spirit dll ?

Posted on 2003-11-14
9
361 Views
Last Modified: 2012-06-27
hi dears,
i want to link my win32 application (non-mfc) to two dlls so i did that:

1)i went to project->settings->link->object/library modules: and added the 1stDll.lib and 2stDll.lib.

2)in this two dlls there is a function each one has the same name and prtotype let's say (void func()) but they are different in code (do different jobs)

3)in my main source file (app.cpp) i imported the two functions that i want to call by writting this code:
__declspec(dllimport) void __stdcall func(); //this is the 1stDll's function
__declspec(dllimport) void __stdcall func(); //this is the 2stDll's function

but the second declreation will make an error while compiling and linking the program so i can not write it ... now how i can call these two functions from my (one .cpp source file) application even if they having the same name and prototype ? is not there any way to alias thier names ? and i can not change the dlls code or names or anything into them becouse they are allready compiled and programmed by another programmer and i do not have thier code LOL... PLZZ help
0
Comment
Question by:Kantaro
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 12

Expert Comment

by:andrewjb
ID: 9746156
You'll probably have to load them manually, via LoadLibrary and so on. Then you'll be storing pointers to the functions, and be able to call them individually.
0
 

Author Comment

by:Kantaro
ID: 9747094
guys... i can not use the LoadLibrary becouse of a perpose in my boss mind LOL .. so i just can load them from the linker and dllimport ....... PLZZ HELP
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 9747126
???

If your boss told you you weren't allowed to use the 'w' key on your keyboard, would you agree? If it ain't going to work without, you've no choice.

:-)
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9747180
Does the functions have parameters?
0
 

Author Comment

by:Kantaro
ID: 9752472
may be and may be not lol ... depend on the implementation but this is a general talk .. okey leave this alone and tell me what is the c/c++ calling conv. ? is it __cdecl ? __stdcall ? and if not __stdcall tell me why in the C++/C project options there is the /Gz comiler option which is the forcing for making functions __stdcall if there is not calling conv. spec. ?
0
 
LVL 6

Accepted Solution

by:
GloomyFriar earned 50 total points
ID: 9753936
__cdecl - is default for C
__stdcall - is default for C++

/Gd, /Gr, /Gz (Calling Convention)See Also
These options determine:

The order in which function arguments are pushed onto the stack.
Whether the caller function or called function removes the arguments from the stack at the end of the call.
The name-decorating convention that the compiler uses to identify individual functions.

/Gd, the default setting, specifies the C calling convention for all functions except C++ member functions and functions marked __stdcall or __fastcall.

/Gr specifies the __fastcall calling convention for all functions except C++ member functions and functions marked __cdecl or __stdcall. All __fastcall functions must have prototypes.

/Gz specifies the __stdcall calling convention for all prototyped C functions except for functions that take a variable number of arguments and functions marked __cdecl or __fastcall. All __stdcall functions must have prototypes.

Note   x86 Specific —>By default, C++ member functions use a calling convention in cases where the member function's this pointer is passed in the ECX register. All other arguments are pushed onto the stack from right to left, and the called routine pops the member function's arguments from the stack. END x86 Specific A member function that is explicitly marked as __cdecl, __fastcall, or __stdcall uses the specified calling convention. A member function that takes a variable number of arguments always uses the __cdecl calling convention.
__cdecl Specifics
For C, the __cdecl naming convention uses the function name preceded by an underscore ( _ ); no case translation is performed. Unless declared as extern "C", C++ functions use a different name-decorating scheme. For more information, see Decorated Names.

__fastcall Specifics
Some of a __fastcall function's arguments are passed in registers x86 Specific —> ECX and EDX END x86 Specific, and the rest are pushed onto the stack from right to left. The called routine pops these arguments from the stack before it returns. Typically, /Gr decreases execution time.

Note   Be careful when using the __fastcall calling convention for any function written in inline assembly language. Your use of registers could conflict with the compiler's use.
For C, the __fastcall naming convention uses the function name preceded by an at sign (@) followed by the size of the function's arguments in bytes. No case translation is done. The compiler uses the following template for the naming convention:

@function_name@number
Note   Microsoft does not guarantee the same implementation of the __fastcall calling convention between compiler releases. For example, the implementation differs between the 16-bit and 32-bit compilers.
When using the __fastcall naming convention, use the standard include files. Otherwise you will get unresolved external references.

__stdcall Specifics
A __stdcall function's arguments are pushed onto the stack from right to left, and the called function pops these arguments from the stack before it returns.

For C, the __stdcall naming convention uses the function name preceded by an underscore ( _ ) and followed by an at sign (@) and the size of the function's arguments in bytes. No case translation is performed. The compiler uses the following template for the naming convention:

_functionname@number
x86 Specific —>

This option has no effect on the name decoration of C++ methods and functions. Unless declared as extern "C", C++ methods and functions use a different name-decorating scheme. For more information, see Decorated Names.

END x86 Specific

To set this compiler option in the Visual Studio development environment

Open the project's Property Pages dialog box. For details, see Setting Visual C++ Project Properties.
Click the C/C++ folder.
Click the Advanced property page.
Modify the Calling Convention property.
To set this compiler option programmatically
0
 

Expert Comment

by:Drew_Benton
ID: 9808150
To answer your original quest:

To the best of my knowledge this is illegal in C++, functions cannot have the same name regardless of where they are loaded from, if theyre in the same project and have the same function signiture (paramater list). Logically, this is pointless, if 2 functions had the same name, how would the program know what to call? The excetion to this however is if the function is in a class, then you could call the functions with the same name. An ex would be:

class C1
{
   void Func()
   {
      // DO this
   }
};

class C2
{
   void Func()
   {
      // DO something diff
   }
};

Then you would need to put these in the 2 different libs and then declare a variable of each class:
C1 var1;
C2 var2;

Then call them in the program:
var1.Func();
var2.Func();

**I know you asked something else as well, but I went ahead and submitted this for anyone else who had the same ques and didnt understand
-Drew
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10286303
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: GloomyFriar {http:#9753936}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

770 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