473,320 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Howto use (and debug) C# code in/from C++ code

Hi :)

I have
(A) An application whose functionality I'm gonna extend with a dll
(B) A bunch of C# code that I wish to use in the dll.
(C) A how-to-interact-with-this-app-specification that requires what has been refered to only as "regular DLL entry points", something that seems to require use of C++. (This is where my understanding of the subject gets a bit fuzzy)

All of this is going to be a rather big project, so I'm trying to find the most "efficient" solution, longterm.

Now, from a barebones C++ project that this application accepts as a valid extension, how do I call functions on my C# code?

I have both the C++ and C# source available, and can make any changes to it necessary. I could even go to C++ altogether instead, but I'm more comfortable in C#.

I'm using Visual Studio Professional 2008, and I'm guessing that's it's not really difficult to call code over the project limits, but I could be wrong.

Also, when I do get this working, how will debugging work? Is there anything "extra" I should take into consideration? "Normally" I just run a my code line by line if I wanna debug something, but maybe that's not possible either because it's a dll, or because I use 2 languages.

I'd appreciate any and all assistance :)
Oct 24 '08 #1
5 1812
Frinavale
9,735 Expert Mod 8TB
Hi :)

(C) A how-to-interact-with-this-app-specification that requires what has been refered to only as "regular DLL entry points", something that seems to require use of C++. (This is where my understanding of the subject gets a bit fuzzy)
I don't know what you are talking about here...are you creating a "how to" readme file for the DLL?

Now, from a barebones C++ project that this application accepts as a valid extension, how do I call functions on my C# code?
Include a reference to your C++ in your project. This will create an Interop object that lets the C# code use the C++ unamaged code.

I have both the C++ and C# source available, and can make any changes to it necessary. I could even go to C++ altogether instead, but I'm more comfortable in C#.
I would stick with what your comfortable with. Why would you choose to write code in two different languages if you didn't have to? I personally find managed code a lot easier to work with than unmanaged code....especially with concerns to memory clean up.

If you use C# only you can automatically generate XML documentation for all of your public functions/members. This XML doc can then be used to create help documents from. Check out Sandcastle and DocProject for components that make this easier than doing it by hand.

Also, when I do get this working, how will debugging work?
In Visual Studio you will be able to Debug your C# code....you'll have to use something else to debug your C++ code.

Is there anything "extra" I should take into consideration?
When calling unmanaged code in managed C# code an Interop Object is created so that the C# code can use the unmanaged code. This Object does not always work 100% and you may have to Marshal your C++ code yourself. It would be best to pick either Managed or Unmanaged code for your project....
Like I said earlier I'm much more fond of managed.

I would strongly recommend researching Interop and what is involved when you have to Marshal before you get started so that you know the problems that can happen when using unmanaged and managed code together.

"Normally" I just run a my code line by line if I wanna debug something, but maybe that's not possible either because it's a dll, or because I use 2 languages.
I'd create another application, a Console app which includes the DLL that can be used for testing.


-Frinny
Oct 24 '08 #2
Hey Frinny, thanks for all you help :)

Include a reference to your C++ in your project. This will create an Interop object that lets the C# code use the C++ unamaged code.
About this, I have to ask, did you turn it around? Or do I completely missunderstand what your saying?
I want my C++ code to call C# functions, not the other way around. :)

Taking a rather trivial example with some pseudo-code.

If I take the C++-dll, and write
Expand|Select|Wrap|Line Numbers
  1. unsigned int Identify() {
  2.   return 2;
  3. }
  4.  
This is "correct" and "works". (As is being recognized as a valid extension to Discipulus, as the program is called)

Now, I add a C# project to the same solution, and do
Expand|Select|Wrap|Line Numbers
  1. namespace MySpace {
  2.     public class DllClass {
  3.         public void Identify {
  4.              return 2;
  5.         }
  6.     }
  7. }
  8.  
And now I wish the C++ version of identify to look like this.
Expand|Select|Wrap|Line Numbers
  1. unsigned int Identify() {
  2.     return C#->MySpace->DllClass->Identify();
  3. }
  4.  
What I need to do is somehow expose the namespace MySpace to the C++ code, right? :)
Oct 27 '08 #3
Frinavale
9,735 Expert Mod 8TB
Hey Frinny, thanks for all you help :)



About this, I have to ask, did you turn it around? Or do I completely missunderstand what your saying?
I want my C++ code to call C# functions, not the other way around. :)
I did get it turned around. I'm not sure why I thought you were calling your C++ code from your C# code...maybe it's because you said you prefer C#...

Anyways, what you need to do is make an Interface so that you can expose the C# members to the C++. You are may to have to write some Marshaling code so that your C++ can use your C# for things like Strings etc. (see the MarshalAs Attribute

Once you have that, you compile your C# code.
After that you will have to register the DLL using regasm (this will create a COM Callable Wrapper (CCW) so that your C++ code knows how to call and use your C# code).

Check out this article ...it has details on how to call C# code from C++ and how to call C++ code from C#.

-Frinny
Oct 27 '08 #4
Sry for taking so long to reply, but thanks for the help. The link looks like it can help me out :)
Nov 3 '08 #5
Frinavale
9,735 Expert Mod 8TB
On more link just in case: Exposing .NET Framework Components to COM

:)

-Frinny
Nov 3 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mike C. Fletcher | last post by:
Every few months I get to working on a Python C extension (built with distutils) and discover a pointer error or the like where I'd like to be able to step into my DLL and see where the extension...
4
by: Josef Sachs | last post by:
Is Andrew Kuchling's regex-to-re HOWTO available anywhere? I've found the following (dead) links on various Web pages: http://py-howto.sourceforge.net/regex-to-re/regex-to-re.html...
4
by: Alexander Eisenhuth | last post by:
Hi alltogether, I use ActivePython 2.4.1 , also the debug part from http://ftp.activestate.com/ActivePython/etc/ and VC 6.0 unter Windows XP. I can't figure out howto debug my c++ extension....
6
by: David B. Bitton | last post by:
I am having a problem deserializing XML when the root node is missing a namespace declaration. My Type has an XmlTypeAttribute with a namespace defined. If I attempt to deserialize the XML, I get...
0
by: Jonathan Wilson | last post by:
Firstly, to get msvcrt.lib, install the .NET framework SDK. The version of msvcrt.lib included there is the exact same one as comes with Visual Studio ..NET 2003. There are some other things that...
7
by: Dan Sikorsky | last post by:
How do you iterate thru a dataset to change money fields to a different value? Here's what I have. My dataset is filled directly from a stored procedure. ' Create Instance of Connection and...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
7
by: dmitrey | last post by:
hi all, can anyone explain howto get function from module, known by string names? I.e. something like def myfunc(module_string1, func_string2, *args): eval('from ' + module_string1 + 'import...
8
by: Goran | last post by:
Hi all, i have some questions/problems about "Factories". My origin is a constructor which needs a string in special format. An example: class Example_t {
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.