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
349 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
Comment Utility
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
Comment Utility
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
Comment Utility
???

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
 
LVL 6

Expert Comment

by:GloomyFriar
Comment Utility
Does the functions have parameters?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Kantaro
Comment Utility
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
Comment Utility
__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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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. …
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

763 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

9 Experts available now in Live!

Get 1:1 Help Now