首先:
1. managed code的项目属性中debug页里,"Enable unmanaged code debugging"选上。
2. unmanaged code项目属性的debug页里,Debugger Type设为Mixed。
其次:
- 如果managed code是dll,unmanaged code是exe,managed code的项目属性中debug页里,start external program设为unmanaged code的exe,然后managed code里F5,需要把unmanaged code项目里的一些需要被debug的文件在managed code项目里打开,以便debug;或者在unmanaged code项目里attach unmanaged code的exe,需要把managed code项目里的一些需要被bebug的文件在unmanaged code项目打开,以便debug.
- 如果managed code是exe,unmanaged code是dll,则直接F5 managed code项目exe进行debug。
注意:
- 如果c++的dll中如此定义:extern __declspec(dllexport) int mySum(int x, int y); 并且 calling convention设为_cdecl
则在c#中调用时,需要[DllImport("dll.dll", EntryPoint = "?mySum@@YGHHH@Z", CallingConvention = CallingConvention.Cdecl)]
public static extern int mySum (int x,int y);
funciton name "?mySum@@YGHHH@Z"可通过denpendency walker查出来。
- 如果c++的dll中如此定义:extern “C" __declspec(dllexport) int mySum(int x, int y); 并且 calling convention设为_stdcall
则在c#中调用时,只需要[DllImport("dll.dll"]
public static extern int mySum (int x,int y);
因为C#默认calling convention就是_stdcall,并且用了extern “C"之后function name不会mangling,还是mySum.