标签:
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace CSharpLibraryNameSpace { // Interface declaration. public delegate int NativeDelegateType(IntPtr pStrInUnicode, int strlen, IntPtr pArgs); [ComVisible(true)] public interface ManagedInterface { int ArraySum(IntPtr pIntArray, int len, ref int sum); int CalltheCallbackFun(IntPtr callbackFnPtr, IntPtr pArgs); }; // Interface implementation. [ComVisible(true)] public class ManagedCSharpClass : ManagedInterface { public int ArraySum(IntPtr pIntArray, int len, ref int sum) { Console.Write("{0}\n", System.Reflection.MethodBase.GetCurrentMethod().Name); sum = 0; unsafe { int* pArray = (int*)pIntArray; for (int i = 0; i < len; i++) sum += pArray[i]; } return 0; } public int CalltheCallbackFun(IntPtr callbackFnPtr, IntPtr pArgs) { string str; str = "牛羊豬雞貓狗"; unsafe { fixed (char* pStr = str) { Console.Write("before call callbackFun ptr={0}\n", callbackFnPtr); //Convert IntPtr to Delegate NativeDelegateType callback = Marshal.GetDelegateForFunctionPointer(callbackFnPtr, typeof(NativeDelegateType)) as NativeDelegateType; callback(Marshal.StringToHGlobalUni(str), str.Length, pArgs); } } return 0; } } }
%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe $(ProjectName)bin\$(ConfigurationName)\$(ProjectName).dll /tlb:$(ProjectName).tlb /codebase
%SystemRoot%\system32\xcopy.exe /Y $(ProjectName)bin\$(ConfigurationName)\$(ProjectName).tlb $(SolutionDir)
#include <locale.h> #include <windows.h> // Import the type library. #import "CSharpLibrary.tlb" raw_interfaces_only using namespace CSharpLibrary; int __stdcall CCallbackFunction(wchar_t *pStrInUnicode, int strlen, void *pArgs) { wprintf(TEXT("%s\n"), pStrInUnicode); wprintf(TEXT("%d\n"), *((int*)pArgs)); return 0; }/*CCallbackFunction*/ int main(int argc, char *argv[]) { HRESULT hr; /*Set current unicode*/ setlocale(LC_ALL,""); // Initialize COM. hr = CoInitialize(NULL); // Create the interface pointer. ManagedInterfacePtr CSharpDLLPtr(__uuidof(ManagedCSharpClass)); { long lResult = 0; int intArray[10], sum; int i; for(i = 0; i< 10; i++) intArray[i] = i; // Call the ArraySum method CSharpDLLPtr->ArraySum((long)&intArray[0], sizeof(intArray), (long*)&sum, &lResult); wprintf(L"The result is %d\n", lResult); }/*local variables*/ { long nCallbackResult; int someNum; someNum = 4; CSharpDLLPtr->CalltheCallbackFun((long)CCallbackFunction, (long)&someNum, &nCallbackResult); wprintf(L"The nCallbackResult is %d\n", nCallbackResult); }/*local variables*/ // Uninitialize COM. CoUninitialize(); return 0; }
ArraySum
The result is 0
before call callbackFun ptr=4264216
牛羊豬雞貓狗
4
The nCallbackResult is 0
// Created by Microsoft (R) C/C++ Compiler Version 14.00.50727.762 (92c42779). // // c:\users\gaiger\desktop\ccallccsharp\debug\csharplibrary.tlh // // C++ source equivalent of Win32 type library CSharpLibrary.tlb // compiler-generated file created 05/08/16 at 01:10:19 - DO NOT EDIT! #pragma once #pragma pack(push, 8) #include <comdef.h> namespace CSharpLibrary { // // Forward references and typedefs // struct __declspec(uuid("406b133d-a839-43fe-882c-d126830250b2")) /* LIBID */ __CSharpLibrary; struct __declspec(uuid("ce8f3e90-5777-330d-9d1b-73e8fb398f2a")) /* dual interface */ ManagedInterface; struct /* coclass */ ManagedCSharpClass; struct __declspec(uuid("b00d90a9-5151-380e-aae8-de1ba2b632e5")) /* dual interface */ _ManagedCSharpClass; // // Smart pointer typedef declarations // _COM_SMARTPTR_TYPEDEF(ManagedInterface, __uuidof(ManagedInterface)); _COM_SMARTPTR_TYPEDEF(_ManagedCSharpClass, __uuidof(_ManagedCSharpClass)); // // Type library items // struct __declspec(uuid("ce8f3e90-5777-330d-9d1b-73e8fb398f2a")) ManagedInterface : IDispatch { // // Raw methods provided by interface // virtual HRESULT __stdcall ArraySum ( /*[in]*/ long pIntArray, /*[in]*/ long len, /*[in,out]*/ long * sum, /*[out,retval]*/ long * pRetVal ) = 0; virtual HRESULT __stdcall CalltheCallbackFun ( /*[in]*/ long callbackFnPtr, /*[in]*/ long pArgs, /*[out,retval]*/ long * pRetVal ) = 0; }; struct __declspec(uuid("585bb79e-7d64-3d56-ab11-70c238098a18")) ManagedCSharpClass; // [ default ] interface _ManagedCSharpClass // interface _Object // interface ManagedInterface struct __declspec(uuid("b00d90a9-5151-380e-aae8-de1ba2b632e5")) _ManagedCSharpClass : IDispatch {}; } // namespace CSharpLibrary #pragma pack(pop)
NativeDelegateType callback = Marshal.GetDelegateForFunctionPointer(callbackFnPtr, typeof(NativeDelegateType)) as NativeDelegateType;
using namespace CSharpLibrary;
标签:
原文地址:http://blog.csdn.net/u013606170/article/details/51357091