Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have successfully created a single file of unmanaged c++ and used it into my c# application with both methods dll import and by creating a wrapper class for it.

Now i have a created a whole project in c/c++ that uses various .lib, .in, .def,.asm, third party dll's and lib's along with my c and header files.

Now my c/c++ project performs very specific time consuming tasks, c++ offers better memory optimization and speed than c#, otherwise i would have created it in c#.

now i have few question's for which i need help

1)If i have two or more files of unmanaged code (c/c++) but i want to only use one file, can i use it without making any necessary changes to the other files which i do not require to be accessed in c#. how would i go about this, should i create a wrapper class around only one file of my c++ code which in turns refer to other files, or should i export the few functions of that file and again leave others files unchanged(if i could do that).

2)when using the soultion of my above question what role would the various files like .lib, .in , .asm would play during my process of using them into managed code.

3)should i remove the dependecies of project to other dll and .lib's or combine them into one dll which references to all of the resources or various dependencies of my project( is doing such a thing is efficient)

4)also how would the communication would take place between my unmanged and managed code on such a large scale.

any help would be appreciated.
Posted

Using unmanaged code in managed is not a big problem, but this is a very unpleasant problem, using P/Invoke and perhaps complex marshaling. You have a very good opportunity to get away from ramming such problems, to resolve problems really painlessly, but it looks like you are ignoring it.

Chances are, you work with Visual Studio and Microsoft compilers. Here is what you can do: write your code in C++/CLI, not "plain" C++. You can create a mixed-mode (managed+unmanaged) project which would produce an executable module of binary use: it can be used as a regular unmanaged DLL; at the same time, it will play the role of the main module of a .NET assembly which can be used by other .NET assemblies.

In a mixed-mode project, you can wrap your existing C++ classes you want to reuse in the managed "ref" classes and structure which will be exposed to the assemblies referencing your assemblies if you just use public access specifiers to the type and their members you want to expose.

Please see: https://msdn.microsoft.com/en-us/library/ms235282.aspx[^].

See also my past answers:
How to invoke C++ DLL in ASP.NET?[^],
Dealing with windows form application[^],
see also: How to play beeps in C#[^].

Some reading on C++/CLI:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^],
https://msdn.microsoft.com/en-us/library/dtefa218.aspx[^].

Did it cover all your questions? Maybe it will take just one extra note: removing any parasitic dependencies is critically important for the health of any products.

—SA
 
Share this answer
 
Comments
RajneeshSaysHello 6-May-15 0:54am    
thank you for answer,and the imp. note about removing any dependencies.
Sergey Alexandrovich Kryukov 6-May-15 0:57am    
Sure. Will you accept this answer formally, too?
—SA
RajneeshSaysHello 6-May-15 1:09am    
i gave it a 5 star :)
Sergey Alexandrovich Kryukov 6-May-15 1:14am    
Thank you. You did not answer my question. Don't you think should accept this answer formally? This is really a better option.
—SA
Hi,
as i understand, basically your problem statement is:
There is a project in Unmanaged C++. You wan to call the functions in this Project in your C# application.
Here are the steps:

It is common practice to write complex or time consuming algorithmic code in C++, to leverage the speed and efficiency it offers.
Then export functions from C++ code which the module will reveal to other modules.
Then build this module as a .dll .
Then add the reference of this .dll to the C# or VB application.
Consume the functionality exposed by C++ .dll by calling the functions exposed by the .dll.
Trying to cover every step in detail will consume a lot of time. You will have to google for more details on any step.
Now there are different methods of exporting functions from a VC++ dll.

1). Number of files do not matter. Its better to build your C++ project as dll . here is a simple walkthrough for creating VC++dll[^].
Kindly google more on these lines. Export only the functions that you will consume from C# application. You can export functions from same file or from separate files. You can export functions only from 1 file while other files will not get disturbed.

2). All the .lib, .in, .def,.asm, and third party dlls will become a dependency for the new VC++ dll you will build. Try to understand what a .lib file and .def file are. The C# application need not bother about these. But to work correctly, the vc++ dll will need these other binaries in a defined location. Google more about how to manage dependencies of the dll.

3). Putting the dependencies in a separate module can be done . I think this is more of a design decision which only you can make since you know how large or complex your project is.

4). I do not know the scale of your project. If you are bothered about latency in calling the C++ dll, then better worry about these optimizations when you have the code working.

here are a few useful links:
How to Marshal a C++ Class[^]
HowTo: Export C++ classes from a DLL[^]
Exposing native to managed - C++/CLI vs. P/Invoke[^]

writing-a-dll-in-c-c-for-net-interoperability[^]

I just tried to give you pointers. A detailed answer would be very lengthy.

hope it helps !!
 
Share this answer
 
v2
Comments
chandanadhikari 5-May-15 12:22pm    
A humble request ~~ whoever downvoted the answer, please point out which part of the answer is incorrect and so that in case the OP tries to follow it, he might not end up spending time in the wrong direction. Explaining why its wrong will also help me in improving my knowledge .
thanks.
RajneeshSaysHello 6-May-15 0:53am    
thank you for clearing my doubts
RajneeshSaysHello 6-May-15 1:07am    
just one more doubt i have, how will memory handling will take place, i mean do i have to clean up the memory from my managed code by calling destructor manually for the all files of my unmanaged code or do i need to clean up the memory only for the called functions/ used variables of my unmanaged code and the other files memory handling will take place just like i defined in my c++ code(which was not called/exported)
chandanadhikari 6-May-15 5:53am    
Hi,
The simple principle is de-allocate memory from where ever you allocated it .


a helpful CodeProject article:
http://www.codeproject.com/Articles/66244/Marshaling-with-C-Chapter-Marshaling-Simple-Type

try to understand how interop marshaler works.
https://msdn.microsoft.com/en-us/library/zah6xy75(v=vs.110).aspx


Please look at the solution of this question.
http://stackoverflow.com/questions/6682011/passing-array-of-strings-from-c-sharp-to-c-memory-management

If something is still not clear feel free to reply to this comment.
RajneeshSaysHello 7-May-15 4:59am    
could you take a look at my new question

http://www.codeproject.com/Questions/989138/Using-cli-wrapper-class-for-c-cplusplus-mixed-code

You created somehow a mess - now you got to cleanup. It is always a bad idea to use different languages and trying to mix them up in the end.

1. use classes or namespace to separate and cleanup your code.
2. include the resulting C++ stuff in as little dll as possible and use them via C#.
3. YES! YES! YES! every removed dependency makes life easier
4. You can use callbacks and delegate to get informed what is going on.

Here is some sample project how it can be done.
 
Share this answer
 
Comments
chandanadhikari 5-May-15 10:37am    
Hello KarstenK, looks like the link to sample project is not correct. Kindly check it. If its false alarm then please excuse me for bothering you..~~thanks
RajneeshSaysHello 6-May-15 1:00am    
thank you for helpful insights towards my problem.

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



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