如何在c ++中调用c#方法? [英] How do you call a c# method in c++?

查看:94
本文介绍了如何在c ++中调用c#方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里在这里他们说说做什么,但我似乎无法找到我的c#项目在c ++。

here and here they do talk about what to do, but i seem to be unable to find my c# project in c++.

我已经添加了c#项目作为c ++项目的引用,但每当我尝试使用我需要的方法,它不能找到命名空间。我已经添加它通过右键单击c + +项目并去'参考'然后添加了c#项目与添加新的参考。两个项目都在同一个解决方案中。

I have added the c# project as a reference in the c++ project but whenever i try to use the method i need, it can't find the namespace. i have added it by right clicking the c++ project and going for 'reference' then added the c# project with add new reference. both projects are in the same solution.

在下面的代码excamples我给了完整的c#代码(除了uses)和c ++代码的一部分其中我试图调用c#方法从)。我还更改了一些命名空间更通用,不包含敏感信息。

In the below code excamples i have given the full c# code (except for usings) and a part of the c++ code (the method where i am trying to call the c# method from). I have also changed some of the namespacing to be more generic and contain no sensitive information.

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;
}


推荐答案

a href =http://msdn.microsoft.com/en-us/library/yab9swk4%28v=vs.80%29.aspx =nofollow>#using directive to the code 。例如,如果你的C#dll命名为 Decrypt.dll ,请将其添加到C ++编译器的顶部:

You need to add a #using directive to the code. For example, if your C# dll were named Decrypt.dll, add this to the top of your C++ compiland:

#using "Decrypt.dll"

请确保调用受管方法的C ++代码也使用 / clr 编译器选项编译为managed。

You also need to make sure the C++ code that calls a managed method is also compiled as managed using the /clr compiler option.

此外,我相信您需要使用 :: 作为命名空间分隔符,而不是

Also, I believe you need to use :: as a namespace separator, rather than ..

lpAlpha2 = Company::Pins::Bank::Decryption::Decrypt::Decryption("123456");

这篇关于如何在c ++中调用c#方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆