none
Unsafe developing in .NET ( C# ) RRS feed

  • Question

  • Hello everybody!

    I want to know the next info:

    There is the possibility in C# to make unmanaged application ( unmanaged code ). And this possibility, as I understand, is used for the work of: unmanaged + managed code.

    For example, as I understand, Silverlight was developed on C++/C# ( and others in .NET etc ), so, in this situation developers, which made Silverlight are using such possibility in C#.

    I want to understand in practice, on the simpliest example, how can I make an application, which uses unamaned C++/C# ( if, we are not using Visual C++ ( C++\CLI ), and we are using GCC-compiler ).

    Be honest, I don't understand, how can I integrate unmanged C++/C#. Please, don't agrue at me, I just studing and just want to know the aim, theory etc

    Let me show you the simple code with unmanaged code in C#.

    using System;
    
    class program
    {
    	//Let's point method Main() as unsafe
    	unsafe public static int Main()
    	{
    		int a = 15;
    
    		int* b; //Make poinet on int-value
    		
    		b = &a; //Place in var "b" the addrees of var "a"
    
    		Console.WriteLine("The 1st value of var a: " + *b); //Print us - 15
    
    		*b = 9;
    
    		Console.WriteLine("The new value of var a: " + *b); //Print us - 9
    
    		return 0;	
    
    	}
    }
    
    Let's compile it with the option /unsafe.

    Pointer - is the variable, which holds the address of other variables.

    Let's return to C++/C#, if I in C++ made the VAR - (int)z, and then make here value = 100.
    Then compile it with GCC this program, which will print the value of (int)z => 100.

    How can I get the address of this variable and get its value in C# and then print it through Console.WriteLine(); ?
    Or how can I get the address of this variable in my program, under CLR work and use it in my C++ program ( which I have compiled not in Visual C++ ).

    PS: please, don't agrue at me =) I just studing and getting new knowladge, I don't know many things in developing, if I have written something wrong, please help me to understand

    Best Regards,
    Oleg
    Saturday, October 17, 2009 8:48 PM

Answers

  • Hello Magesi

    1). Find how to create dll in C++/GCC
    2). Make in this DLL the expoting method ( dllexport ), which will return a value of variable.


    This forum convers only Microsoft products, e.g. Visual C++, so my answer is for VC++. You can post the question to a gcc forum if you need a gcc solution.

    Exporting from a DLL
    http://msdn.microsoft.com/en-us/library/z4zxe9k8.aspx

    Exporting from a DLL Using DEF Files
    http://msdn.microsoft.com/en-us/library/d91k01sh.aspx

    Exporting from a DLL Using __declspec(dllexport)
    http://msdn.microsoft.com/en-us/library/a90k134d.aspx

    Creating and Using a Dynamic Link Library (C++)
    http://msdn.microsoft.com/en-us/library/ms235636.aspx

    HOWTO: How To Export Data from a DLL or an Application
    http://support.microsoft.com/kb/90530

    If you want a working example, please refer to the CppDllExport sample in Microsoft All-In-One Code Framework. In ReadMe.txt file of the sample, you can find the detailed steps of creating such a C++ dll exporting various symbols.

    3). Find how in C# I can call methods from unmanaged dll ( let's look at DllImportAttibute ).

    Platform Invoke Tutorial
    http://msdn.microsoft.com/en-us/library/aa288468.aspx

    Using P/Invoke to Call Unmanaged APIs from Your Managed Classes
    http://msdn.microsoft.com/en-us/library/aa719104.aspx

    Calling Win32 DLLs in C# with P/Invoke
    http://msdn.microsoft.com/en-us/magazine/cc164123.aspx

    If you want a working example, please refer to the CSPInvokeDll sample in Microsoft All-In-One Code Framework. The sample pinvokes the above-mentioned CppDllExport.dll.

    Please feel free to tell me if you have any other questions.

    Regards,
    Jialiang Ge


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Yort Monday, October 19, 2009 3:14 AM
    • Marked as answer by magesi Tuesday, October 20, 2009 7:46 AM
    Monday, October 19, 2009 3:11 AM
    Moderator
  • Hi,

    There might be an answer to your question, but I don't know what it is. I would like to clarify a few things though...

    1. You cannot write 'unmanaged code' in C#... you can call unmanaged functions in a C/C++ dll from C#, but C# only writes/builds manage code. You can also write 'unsafe' code in C#, but that is not the same as 'unmanaged' code and doesn't have much to do with C/C++ integration.

    2. C# doesn't have pointers, not like C++ anyway. To do what you want, you're probably going to have to put the code that uses a pointer in a C++ method in an unmanaged dll and then call that method from C#... the result will be returned in a managed data type (i.e a managed int in your example) and then you would use it like any other managed type.

    The Silverlight team probably uses a combination of managed and unmanaged code, but not inside C#... they will have some unmanaged code written in C++ and some managed code written in C++ .Net and/or C#. The C# code will be able to call the unmanaged C++ code if it needs to, and vice-versa, but the unmanaged code isn't written in C# itself.

    Calling unmanaged code from C# is usually refered to a PInvoke (short for platform-invoke). Google for C# and PInvoke to see how to declare the unmanaged functions so they can be called.

    Hope that helps.
    • Marked as answer by magesi Tuesday, October 20, 2009 7:46 AM
    Saturday, October 17, 2009 9:10 PM

All replies

  • Hi,

    There might be an answer to your question, but I don't know what it is. I would like to clarify a few things though...

    1. You cannot write 'unmanaged code' in C#... you can call unmanaged functions in a C/C++ dll from C#, but C# only writes/builds manage code. You can also write 'unsafe' code in C#, but that is not the same as 'unmanaged' code and doesn't have much to do with C/C++ integration.

    2. C# doesn't have pointers, not like C++ anyway. To do what you want, you're probably going to have to put the code that uses a pointer in a C++ method in an unmanaged dll and then call that method from C#... the result will be returned in a managed data type (i.e a managed int in your example) and then you would use it like any other managed type.

    The Silverlight team probably uses a combination of managed and unmanaged code, but not inside C#... they will have some unmanaged code written in C++ and some managed code written in C++ .Net and/or C#. The C# code will be able to call the unmanaged C++ code if it needs to, and vice-versa, but the unmanaged code isn't written in C# itself.

    Calling unmanaged code from C# is usually refered to a PInvoke (short for platform-invoke). Google for C# and PInvoke to see how to declare the unmanaged functions so they can be called.

    Hope that helps.
    • Marked as answer by magesi Tuesday, October 20, 2009 7:46 AM
    Saturday, October 17, 2009 9:10 PM
  • Thanks, for a piece of advice. I have searched something and made some meanings about it.

    Calling methods, which are developed on C++ from C# - it's not "unmanaged programming ( development )", it just integration with unamanaged code, for that, there are a ot of classes in System.Runtim.InteropServices.

    The solution of my problem is:

    1). Find how to create dll in C++/GCC.
    2). Make in this DLL the expoting method ( dllexport ), which will return a value of variable.
    3). Find how in C# I can call methods from unmanaged dll ( let's look at DllImportAttibute ).
    Saturday, October 17, 2009 11:21 PM
  • Hello Magesi

    1). Find how to create dll in C++/GCC
    2). Make in this DLL the expoting method ( dllexport ), which will return a value of variable.


    This forum convers only Microsoft products, e.g. Visual C++, so my answer is for VC++. You can post the question to a gcc forum if you need a gcc solution.

    Exporting from a DLL
    http://msdn.microsoft.com/en-us/library/z4zxe9k8.aspx

    Exporting from a DLL Using DEF Files
    http://msdn.microsoft.com/en-us/library/d91k01sh.aspx

    Exporting from a DLL Using __declspec(dllexport)
    http://msdn.microsoft.com/en-us/library/a90k134d.aspx

    Creating and Using a Dynamic Link Library (C++)
    http://msdn.microsoft.com/en-us/library/ms235636.aspx

    HOWTO: How To Export Data from a DLL or an Application
    http://support.microsoft.com/kb/90530

    If you want a working example, please refer to the CppDllExport sample in Microsoft All-In-One Code Framework. In ReadMe.txt file of the sample, you can find the detailed steps of creating such a C++ dll exporting various symbols.

    3). Find how in C# I can call methods from unmanaged dll ( let's look at DllImportAttibute ).

    Platform Invoke Tutorial
    http://msdn.microsoft.com/en-us/library/aa288468.aspx

    Using P/Invoke to Call Unmanaged APIs from Your Managed Classes
    http://msdn.microsoft.com/en-us/library/aa719104.aspx

    Calling Win32 DLLs in C# with P/Invoke
    http://msdn.microsoft.com/en-us/magazine/cc164123.aspx

    If you want a working example, please refer to the CSPInvokeDll sample in Microsoft All-In-One Code Framework. The sample pinvokes the above-mentioned CppDllExport.dll.

    Please feel free to tell me if you have any other questions.

    Regards,
    Jialiang Ge


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Yort Monday, October 19, 2009 3:14 AM
    • Marked as answer by magesi Tuesday, October 20, 2009 7:46 AM
    Monday, October 19, 2009 3:11 AM
    Moderator
  • Hi,

    The links given by Jialiang are correct, also it's NOT CLI it's CLR (Common Language Runtime), the C++ you spoke about 
    is the one which is based on managed code (while normal C/C++ are based on so called "unmanaged" code, where the 
    programmer needs to write the "destructor" while in managed we have GC (Garbage Collector) which is a destructor. 

    GCC (GNU C Compiler), why are you using that?
    Microsoft Visual Studio works perfect to use in something like this. 

    BTW, do NOT forget to follow and accept the GPL (GNU Public License). 

    Also I must add, In C++ you can make a pointer and use the operator & (by adding the AND operator you're
    able to retrieve the memory address for that variable).

    Like this:
    using System;
    
    class program
    {
    	//Let's point method Main() as unsafe
    	unsafe public static int Main()
    	{
    	         int a = 15;
                      int* b; //Make poinet.
    		b = &a; 
    Console.WriteLine("The address of variable is: " + b); //0x1c9832. return 0;
    } }

    Code-Snippet: The above code will print the address of variable.

    Note: If you print it like this *b = then you will see the value, however, if you let it as it is b = then you
    get the memory address.

    Have a nice day...

    Best regards,
    Fisnik  
    Coder24.com
    Wednesday, October 21, 2009 1:48 PM