C# and C++ interop question
-
I have an application written in C++ that allows me to write addons. It has a public .h file with some class definitions. I can create .dll that has known entry point and that entry point will receive as one of it's parameters pointer to internal class from the app. Now I have C# library (that I use in other projects) and I want to use it within the application. My first thought was to use regasm and use COM to do all the interop. Might work, but I've never done any COM from C++ and it looks a bit messy. My second approach would be to use managed C++ (which I haven't tried either). So my question is: If I write my .dll with managed c++ and use my library (written in C#), will the application be able to load the library? How will CLR be loaded? This lib is written for .net 2, and I can use the new CLI C++, which looks nicer than 1.0 Also, If I have static objects within my library, will those be loaded upon each call? Thanks.
-
I have an application written in C++ that allows me to write addons. It has a public .h file with some class definitions. I can create .dll that has known entry point and that entry point will receive as one of it's parameters pointer to internal class from the app. Now I have C# library (that I use in other projects) and I want to use it within the application. My first thought was to use regasm and use COM to do all the interop. Might work, but I've never done any COM from C++ and it looks a bit messy. My second approach would be to use managed C++ (which I haven't tried either). So my question is: If I write my .dll with managed c++ and use my library (written in C#), will the application be able to load the library? How will CLR be loaded? This lib is written for .net 2, and I can use the new CLI C++, which looks nicer than 1.0 Also, If I have static objects within my library, will those be loaded upon each call? Thanks.
Hi, My suggestion is , Create Com (ActiveX Dll),using ATL.It's very simple .And u Don't Have To Break Heads On it. Start -> ATLCOMAPPWIZARD -> Create Simple Object , This Will Your Interface from Outside.Okay, Now, Create Methods Exactly With Same As In Your Class written C++ [ ".cpp And .h"]. Each Methods In The Interface Will Actually wraps The Methods In Your .cpp . For Example :- Your C++ Class Implements add Fuction YourClass.add(long a, long b) you just call this method from YourDll.Add(long a,long b) create the dll. u can add this dll into C# project.By Add reference Method [Note:- U Can Use This In Any Language that Supports COM] Okay.Any doubts feel free to contanct me by baijumax
-
I have an application written in C++ that allows me to write addons. It has a public .h file with some class definitions. I can create .dll that has known entry point and that entry point will receive as one of it's parameters pointer to internal class from the app. Now I have C# library (that I use in other projects) and I want to use it within the application. My first thought was to use regasm and use COM to do all the interop. Might work, but I've never done any COM from C++ and it looks a bit messy. My second approach would be to use managed C++ (which I haven't tried either). So my question is: If I write my .dll with managed c++ and use my library (written in C#), will the application be able to load the library? How will CLR be loaded? This lib is written for .net 2, and I can use the new CLI C++, which looks nicer than 1.0 Also, If I have static objects within my library, will those be loaded upon each call? Thanks.
Without using managed c++, also if your apis exposed do not have complex data structures used, you can export the dll and straight away marshal these apis to your own defined api and use it in your code using pinvoke. For e.g. [DllImport("Advapi32.dll", CharSet=CharSet.Auto)] static extern Boolean FileEncryptionStatus(String filename, out UInt32 status); Try some search on pinvoke. - Shailesh
-
Hi, My suggestion is , Create Com (ActiveX Dll),using ATL.It's very simple .And u Don't Have To Break Heads On it. Start -> ATLCOMAPPWIZARD -> Create Simple Object , This Will Your Interface from Outside.Okay, Now, Create Methods Exactly With Same As In Your Class written C++ [ ".cpp And .h"]. Each Methods In The Interface Will Actually wraps The Methods In Your .cpp . For Example :- Your C++ Class Implements add Fuction YourClass.add(long a, long b) you just call this method from YourDll.Add(long a,long b) create the dll. u can add this dll into C# project.By Add reference Method [Note:- U Can Use This In Any Language that Supports COM] Okay.Any doubts feel free to contanct me by baijumax
-
Without using managed c++, also if your apis exposed do not have complex data structures used, you can export the dll and straight away marshal these apis to your own defined api and use it in your code using pinvoke. For e.g. [DllImport("Advapi32.dll", CharSet=CharSet.Auto)] static extern Boolean FileEncryptionStatus(String filename, out UInt32 status); Try some search on pinvoke. - Shailesh
-
The app has several quite big C++ abstract classes. My dll will get loaded and passed one of the classes (actually some object derived from this class).
I am not sure i understood you, you want to import the class or class static functions? Could you elaborate a little more? - Shailesh