views:

240

answers:

3

Please tell me how to call a unmanaged c++ exe functions from managed c++ exe or dll? help with code example would be more useful. Thank you

A: 

You need to link against the library that contains the function you want to call, include a header that defines the function and then just call it.

Without a more specific question you're not going to get much more than that.

Stu Mackellar
Problem here is Lib file is not generated for exe. I have an unmanaged c++ exe, its header(test.h) is below#include "stdafx.h"class MyClass{public: int Mytest(int data);};and below is test.cpp#include "stdafx.h"#include "test.h"int MyClass::Mytest(int data){ return 100;}int main(int argc, char* argv[]){ return 0;}Now i want to call this MyTest() function from a managed c++ Dll.
Maestro
A: 

I think you should have a look at P/Invoke. Using this tech you could call any unmanged function exported in a DLL or EXE from a managed function.

For example: http://www.codeproject.com/KB/cs/essentialpinvoke.aspx

Andreas Roth
Thank you Andreas for reply. P\Invoke is again usefull for dll not for exe. attribute DllImport looks for the dll. but i have a exe.I am not getting any clue how to do this.
Maestro
I do not think you can call function in exe from another exe..
Betamoo
It's useful for exe as well. But you're right that the function in question must be exported from the exe file. Typically an exe file do not export any symbols.Are you interested in calling a private function for the exe file?
Andreas Roth
A: 

The managed/unmanaged is a red herring. When you have some code you want to call, it should be in a lib, a DLL, or a COM exe. A regular double-click-it-to-run-it exe that doesn't implement any COM interfaces doesn't expose any of its code to outside callers. If you just want to run it, you can use Process.Start to launch the whole exe. Otherwise you're going to need to re-architect a bit (this will involve having the source code to the other exe.) Generally I pull most of the functionality into a lib or dll, have the original exe call into that library to get its work done, and have the new exe also call into the same library.

Since you're in C++/CLI, do not go COM Interop or P/Invoke. IJW is way easier (it just works, right?) Include the header, link to the lib. Done! But as you now see, getting the lib can be the big first step.

Kate Gregory
Thank you Kate.It seems the way i wanted to do it is not possible. I will re-architect it now.
Maestro