简体   繁体   中英

Attempting to access byte array in C# from C++ Dll

So I am trying to push an unsigned char array from a C++ DLL and wanting to access it in a C# GUI. I set up the C# as an IntPtr and I am trying to Marshal.Copy the byte array but the IntPtr is always NULL, what amI diong wrong!

Any help would be greatly appreciated.

Below is the code:

Portion of the C++ DLL Header

class TEST_API TestClass
{
public:
TestClass(void);
~TestClass(void);

void GetData( unsigned char * testData, unsigned size );

unsigned char testInfo[4096];
};
#ifdef __cplusplus
extern "C" {
#endif
extern TEST_API TestClass* CreateTestClass();
extern TEST_API void DisposeTestClass(TestClass* pObject);
extern TEST_API void TestGetData( TestClass* pObject, unsigned char * testData );
#ifdef __cplusplus
}
#endif

This is the C++ DLL portions, it was too much code to put the whole thing

extern "C" TEST_API void TestGetData( TestClass* pObject, unsigned char * image )
{
   if(pObject != NULL)
      pObject->GetData( image );
}
void TestClass::GetData( unsigned char * testData )
{
   for ( int i=0; i < 64; i++ )
      for ( int j=0; j < 64; j++ )
             testInfo[j + i*64] = j;
   testData = testInfo;
}

C# code I did is the follow:

IntPtr trial = new IntPtr();
testing.RetrieveTestData(trial, 64, 64);

byte[] managedArray = new byte[64*64];
Marshal.Copy(trial, managedArray, 0, 64*64);

for (int i = 0; i < 64; i++)
   for (int j = 0; j < 64; j++)
      Console.WriteLine(managedArray[j + i * 64]);

Basically the trial IntPtr is 0 everytime

I hope someone can help me through this. Thanks

trial is passed to RetrieveTestData as an input parameter - meaning it cannot be changed by the caller. To be changed by the caller the parameter must be either ref or out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM