Results 1 to 7 of 7
Thread: C++ <-> C++/CLI
Hybrid View
-
13th September 2009 22:18 #1
C++ <-> C++/CLI
C#, C++.
DLL, C++( unmanaged ), C# .
C++ .NET, . , , . DLL- /clr C# . - DLL C# .
4 , .
DLL unmanaged . C# dll- managed , .
:
C# :Code:void MyClass::func( System::String^ str ) { char* str2 = (char*)Marshal::StringToHGlobalAnsi(str).ToPointer(); printf( str2 ); // works // free the memory for str2... // .... }
, - str1 , DLL-.Code:class Start { public static void Main() { MyClass var = new MyClass(); string str1 = "managed call"; var.func(str1 ); } }
- - , 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.
-
14th September 2009 00:11 #2
, . .

, ,
, . .
"640K ught to be enough for anybody" - Bill Gates, 1981
::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel
-
14th September 2009 01:15 #3
C API- :
DLL
C#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
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'); } }
-
14th September 2009 02:51 #4
@: , DLL, - Reference , .. Object Browser-
. DLL C++ .NET, managed. DLL-. , C++ .NET 
@anrieff: , , char* <-> String^ , .
...
Unmanaged -> Managed:
Managed -> Unmanaged( MSDN ):Code:char* str3 = "Unmanaged string"; String^ res = Marshal::PtrToStringAnsi( (IntPtr)str3 ); // => res = "Unmanaged String"
managed string , C++.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));
-, C++ .NET out, C#:
C++ .NET
C# :Code:void MyClass::func( [Runtime::InteropServices::Out] String^ %param ) { param = "new value"; ... }
Code:MyClass var = new MyClass(); string param = "managed string"; var.func( out param );
,
. . , ...
.
-
14th September 2009 03:44 #5
, , 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|
-
14th September 2009 13:07 #6
-
14th September 2009 14:42 #7




Reply With Quote

Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in