///////////////////////////////////////////////////////////////////// // Marshaling.cpp - Demonstrates use of marshaling and PInvoke // // // // Jim Fawcett, CSE775 - Distributed Objects, Spring 2003 // ///////////////////////////////////////////////////////////////////// // Built with Win32 Project wizard // // - place MarshalingLib.dll in project and debug directories // ///////////////////////////////////////////////////////////////////// //#using //#using "c:/su/CSE775/code/managedCPP/MarshalingLib/Debug/MarshalingLib.dll" using namespace System; using namespace System::Runtime::InteropServices; #include #include #include "IMarshalingLib.h" using namespace std; // use dumpbin /EXPORTS marshalinglib.dll to find entry point [ DllImport( "MarshalingLib.dll", CharSet=CharSet::Ansi, EntryPoint="?demoTransfer@CMarshalingLib@@UAEXPBD@Z" ) ] extern "C" void CallDemoTransfer([MarshalAs(UnmanagedType::LPStr)] String^ pMsg); [ DllImport("MarshalingLib.dll")] extern "C" void CreateMLobj(IMarshalingLib*& pObj); [ DllImport("MarshalingLib.dll")] extern "C" void DeleteMLobj(IMarshalingLib* pObj); int main() { cout << "\n Demonstrate PInvoke with Specified Marshaling " << "\n ===============================================\n"; cout.flush(); IMarshalingLib* pMLobj; CreateMLobj(pMLobj); String^ pMsg = gcnew String("Hello from Managed World"); CallDemoTransfer(pMsg); pMLobj->demoTransfer("Hello again"); DeleteMLobj(pMLobj); cout << "\n\n"; }