3

here and here they do talk about what to do, but i seem to be unable to find my c# project in 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.

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.

the c# code is like this.

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++ code

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;
}
Community
  • 1
  • 1
Andy
  • 2,248
  • 7
  • 34
  • 57

1 Answers1

4

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"

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");
Andrew Brown
  • 4,086
  • 1
  • 24
  • 21
  • sorry if i am sounding dumb, but adding the clr gives me '/RTC1' and '/clr' command-line options are incompatible. It seemed like a good solution to delete the /RTC1 one butthe box where these are is greyed out and i can't change them. – Andy Apr 14 '11 at 15:53
  • i managed to find out they where options set in other parts of the preferences. i got a problem with this one though: error D8016: '/MT' and '/clr' command-line options are incompatible. this error happened alot of times but in this case i can't get rid of the /MT and all other options seem to give an error. – Andy Apr 14 '11 at 16:02
  • googling upon this it seems i need to use /MD and #define _AFXDLL. – Andy Apr 14 '11 at 16:09