【发布时间】:2011-04-14 15:34:26
【问题描述】:
here 和 here 他们确实在谈论要做什么,但我似乎无法在 c++ 中找到我的 c# 项目。
我已将 c# 项目作为引用添加到 c++ 项目中,但每当我尝试使用所需的方法时,它都找不到命名空间。我已经通过右键单击 c++ 项目并选择“参考”来添加它,然后通过添加新参考添加了 c# 项目。两个项目都在同一个解决方案中。
在下面的代码示例中,我给出了完整的 c# 代码(除了 usings)和部分 c++ 代码(我试图从中调用 c# 方法的方法)。我还更改了一些命名空间,使其更加通用且不包含敏感信息。
c#代码是这样的。
namespace Company.Pins.Bank.Decryption
{
public class Decrypt
{
[DllImport("decryptsn.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr decryptsn(byte[] InpData);
//__declspec(dllimport) char* decryptsn(char* InpData);
public static String Decryption(string param2)
{
byte[] InpData = new byte[255];
InpData = StrToByteArray(param2);
return Marshal.PtrToStringAnsi(decryptsn(InpData));
}
public static byte[] StrToByteArray(string str)
{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetBytes(str);
}
}
}
C++ 代码
CPReSInterfaceApp theApp;
extern "C" DllExport BOOL WINAPI UserInstruction(
HWND hWnd, HINSTANCE hInst, double* lpNumeric,
TCHAR* lpAlpha1, TCHAR* lpAlpha2)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if (lpNumeric == NULL || lpAlpha1 == NULL || lpAlpha2 == NULL)
return FALSE;
ReconcileUHParameter(lpNumeric, lpAlpha1, lpAlpha2);
int iCommand = (int)lpNumeric[0];
lpNumeric[0] = 6;
lpAlpha2 = Company.Pins.Bank.Decryption.Decrypt.Decryption("123456");
return TRUE;
}
【问题讨论】:
标签: c# c++ visual-studio-2010 visual-c++ reference