Results 1 to 7 of 7

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User genn's Avatar
    Join Date: Feb:2003
    Location:
    Posts: 1,215

    C++ <-> C++/CLI

    C#, C++.
    DLL, C++( unmanaged ), C# .
    C++ .NET, . , , . DLL- /clr C# . - DLL C# .
    4 , .
    DLL unmanaged . C# dll- managed , .

    :

    Code:
    void MyClass::func( System::String^ str )
    	{
    		char* str2 = (char*)Marshal::StringToHGlobalAnsi(str).ToPointer();
    
    		printf( str2 ); // works
    
                    // free the memory for str2...
                    // ....
    	}
    C# :

    Code:
    class Start
    {
        public static void Main()
        {
            MyClass var = new MyClass();
    
            string str1 = "managed call";
    
            var.func(str1 );
    
        }
    }
    , - str1 , DLL-.

    - - , C++/CLI .

    DLL managed , wrapper API USB , C.

    . MyClass DLL- managed. , garbage collectora unmanaged ?
    IDisposable ?
    ,

    ---------- 21:18 ---------- 17:41 ----------

    -- --
    Last edited by genn; 13th September 2009 at 20:33.

  2. #2
    ɐ-əpoɔᴉu⋂ ɐ ə anrieff's Avatar
    Join Date: Apr:2004
    Location: Sofia
    Posts: 8,448
    , . .

    , ,
    , . .
    "640K ught to be enough for anybody" - Bill Gates, 1981
    ::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel

  3. #3
    Registered User 's Avatar
    Join Date: Feb:2006
    Location:
    Posts: 653
    C API- :
    DLL
    Code:
    extern "C" 
    {
    
    //Note: must use __declspec(dllexport) to make (export) methods as 'public'
    
          __declspec(dllexport) void DoSomethingInC(unsigned short int ExampleParam, unsigned char AnotherExampleParam)
    
          {
    
                printf("You called method DoSomethingInC(), You passed in %d and %c\n\r", ExampleParam, AnotherExampleParam);
    
          }
    
    }//End 'extern "C"' to prevent name mangling
    C#
    Code:
    class Program
        {
            [DllImport("Win32.dll", EntryPoint = "DoSomethingInC")]
            public static extern void DoSomethingInC(ushort ExampleParam, char AnotherExampleParam);
    
    
            static void Main(string[] args)
            {
                Program.DoSomethingInC(15, 'W');
            }
        }

  4. #4
    Registered User genn's Avatar
    Join Date: Feb:2003
    Location:
    Posts: 1,215
    @: , DLL, - Reference , .. Object Browser- . DLL C++ .NET, managed. DLL-. , C++ .NET

    @anrieff: , , char* <-> String^ , .

    ...
    Unmanaged -> Managed:
    Code:
    char* str3 = "Unmanaged string";
    		String^ res = Marshal::PtrToStringAnsi( (IntPtr)str3 ); 
                    // => res = "Unmanaged String"
    Managed -> Unmanaged( MSDN ):
    Code:
    // Create a managed string.
    		String^ managedString = "Managed String";
    
    		// Marshal the managed string to unmanaged memory.
    		char* stringPointer = (char*) Marshal::StringToHGlobalAnsi(managedString ).ToPointer();
    
    		printf( stringPointer ); // "Managed String"
    
    
    		// Always free the unmanaged string.
    		Marshal::FreeHGlobal(IntPtr(stringPointer));
    managed string , C++.
    -, C++ .NET out, C#:

    C++ .NET
    Code:
    void MyClass::func( [Runtime::InteropServices::Out] String^ %param )
    {
           param = "new value";
    ...
    }
    C# :
    Code:
    MyClass var = new MyClass();
    string param = "managed string";
    var.func( out param );

    , . . , ... .

  5. #5
    Bombera's Avatar
    Join Date: Jul:2001
    Location: 4EVA
    Posts: 13,833
    , , Object Browser-a . o - COM C

    ,
    !
    EVGA X299 FTW K|i9-7960X@4.7|4x8 Patriot Viper Steel 4000|GTX 1660 Ti|970 EVO 1 TB|Seasonic Focus GX-1000|Xigmatek Elysium|
    Rampage IV Extreme BE|E5-1680v2@4.7|4x4 HyperX 1866|Cougar Aqua 240|GTX 1050 Ti|970 EVO 1/4 TB|CM 850 SilentPro|HAF-X|

  6. #6
    Registered User 's Avatar
    Join Date: Feb:2006
    Location:
    Posts: 653
    @genn , C++ C C++ C#. , .

  7. #7
    Registered User genn's Avatar
    Join Date: Feb:2003
    Location:
    Posts: 1,215
    , ... .
    C++ , .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |