Passing parameters from C# class to C++ class.

netnav
netnav used Ask the Experts™
on
I need to call a method in a C++ class from a C# project.  The c++ method works great accept when I try to pass in a parameter.  The C++ signature looks like this:  

usinged char computeCrc(char slideIdString[])
{

}

The C# project see's the parameter as an sbyte*.  I know very little about c++.  Can someone help me figure out how to pass a parameter to this function.

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2012
Commented:
Usually, you'd use a P/Invoke and a StringBuilder, e.g.

http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/ pretty much targets this issue. You could do that like

C++ DLL ("returnstring.dll"):  

#include <windows.h>
#include <tchar.h>

int GetString(LPSTR buf, int nMaxCount)
{
      TCHAR* pszText = _T( "Hi from unmanaged code");
      int nLen = _tcslen(pszText);

      if (nLen < nMaxCount)
      {
         _tcscpy(buf,pszText);

         return nLen;
      }

      return 0;
}

C#

using System.Text; // for StringBuilder
public class ReturnStringDll{
  [DllImport("returnstring.dll")]
  public static extern int GetString(StringBuilder buf, int nMaxCount);
}

StringBuilder cb = new StringBuilder(256);
ReturnStringDll.GetString(sb, sb.Capacity)

Or, in your example

C/C++

usinged char computeCrc(char slideIdString[])
{

}

C#

using System.Text; // for StringBuilder
public class computeCrcDll{
  [DllImport("computeCrc.dll")]
  public static extern usinged char computeCrc(StringBuilder slideIdString);
}

Author

Commented:
I used the DLLImport.  When I call the function I get the following error: Unable to find an entry point named 'computeCrc' in DLL 'CRC32V2.dll'.  The dll is a class library that I created. the computeCrc function is in a class called Class1.  Is this why it is not working.
jkr
Top Expert 2012

Commented:
How exactly is that function built into the library? It should be something aolng the lines of

extern "C" __declspec(dllexport) usinged char computeCrc(char slideIdString[]);
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Author

Commented:
I think I'm getting in over my head with c++.  My original problem is that I was sent a C++ function that generates a CRC.  I have tried to convert the code to C# and have it generate the same CRC given the same input.  However, I have been unable to accomplish this.  I then thought it might be easier to just put the code in a c++ class library project and pass a parameter to it. I have never done any work in C++ and I'm not sure if I'm close to solving this problem or not.  The CRC function is below:

unsigned char computeCrc(char slideIdString[])
{                              
//char slideIdString[] = "12345678911";
                  unsigned char Local8BitPoly = 0x025;
                  unsigned char crcTable[256];
                  int i, j;
                  unsigned char crc = 0;

                  unsigned char crc_accum;
                  for(i=0; i<256; i++)
                  {
                        crc_accum = (unsigned char)i;

                        for(j=0;j<8;j++)
                        {
                              if(crc_accum & 0x80)
                                    crc_accum = (crc_accum << 1) ^ Local8BitPoly;
                              else
                                    crc_accum = (crc_accum << 1);
                        }
                        crcTable[i] = crc_accum;
                        System::Console::WriteLine(crcTable[i]);
                  }

                  for(j=0; j<11; j++)
                  {
                        i=((crc)^slideIdString[j]) & 0xff;
                        crc = (crc << 8)^crcTable[i];
                  }                  

                  return crc;
            }

I'm not sure which way to tackle this problem.  If I can create a dll and pass a parameter without spending a week learning how then OK.  If I'm in over my head then I will go back to rewriting the code in C#. Your advice is appreiciated.
jkr
Top Expert 2012

Commented:
It is quite likely that C++ name decoration is kicking in here. Can you just make that

extern "C" __declspec(dllexport) unsigned char computeCrc(char slideIdString[])
{    

//...

}

?

Author

Commented:
I used your suggestion and got an error that I don't no what to do about.  The code is below as well as the error.

// CRC32V2.h

#pragma once
#include <string.h>

using namespace System;
using namespace System::Runtime::InteropServices;

namespace CRC32V2 {

      public ref class Class1
      {            
            extern "C" __declspec(dllexport) unsigned char computeCrc(char slideIdString[])  
            {                              
                  //char slideIdString[] = "12345678911";
                  unsigned char Local8BitPoly = 0x025;
                  unsigned char crcTable[256];
                  int i, j;
                  unsigned char crc = 0;

                  unsigned char crc_accum;
                  for(i=0; i<256; i++)
                  {
                        crc_accum = (unsigned char)i;

                        for(j=0;j<8;j++)
                        {
                              if(crc_accum & 0x80)
                                    crc_accum = (crc_accum << 1) ^ Local8BitPoly;
                              else
                                    crc_accum = (crc_accum << 1);
                        }
                        crcTable[i] = crc_accum;
                        System::Console::WriteLine(crcTable[i]);
                  }

                  for(j=0; j<11; j++)
                  {
                        i=((crc)^slideIdString[j]) & 0xff;
                        crc = (crc << 8)^crcTable[i];
                  }                  

                  return crc;
            }
      };
}

Error      2      error C2334: unexpected token(s) preceding '{'; skipping apparent function body      c:\documents and settings\administrator.ypiilab\my documents\visual studio 2005\projects\crc32v2\crc32v2\CRC32V2.h      14      

Thanks, for your help.

jkr
Top Expert 2012

Commented:
???

That is not C++, sorry...
jkr
Top Expert 2012

Commented:
I am not quite sure why you want to delete this Q...
jkr
Top Expert 2012

Commented:
Err, what? Is this an invitation to object again or is objecting null and void, since noone cares about it anyway?

Commented:
jkr,
If you have a specific recommendation for resolution of this question, then please make it.
Obviously, you know a great deal more about this Zone than anyone else on EE, but without input there is no way for us to select a comment as the solution.

Are you saying (in the other question) that http:#a19826300 is the solution?

I will stop the auto-close with this post.

Vee_Mod
Experts Exchange Moderator
jkr
Top Expert 2012

Commented:
Yes, from my point of view, that should be the solution.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial