Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It seems that in c# we should use Collection to avoid using dynamic array.
However, I am now using C# to use some unmanaged code in native C++ through a Managed C++ layer and encountered a problem here.

In my C++ code I have a loadFile function:

loadFile(const char* filename, byte* buffer)

which load the file content into the buffer.
I must use this function to load the file since it is compressed and encrypted.
I can't modify the code of the native c++.

I use this approach to load the file:
In c#:

ManagedDataFile DataFile = new ManagedDataFile(@"myPath");
DataFile.openDataFile();

byte[] filebuf = new byte[1024*30];
managedDataFile.loadFile(@"MyFileName", ref filebuf);


However, I can't control the memory allocation of the buffer "filebuf".
Can anyone suggest idea(s) for my situation?
Thanks a lot!
Posted

Write a managed C++ method which returns the size of the file.
 
Share this answer
 
You may want to look at the functions available in the System.Runtime.InteropServices.Marshal class and see if any of them may be of use.
Take a look at this article[^] it may help with your requirement.

Not sure if this will work but you could try this;
C#
IntPtr fileData_ptr = IntPtr.Zero;
try
{
  managedDataFile.loadFile(@"MyFileName", fileData_ptr);
  if (fileData_ptr != IntPtr.Zero)
  {
    IntPtr pData = managedDataFile.GlobalLock(fileData_ptr);
    IntPtr pArrayLocation = pData;

    Int32 uiSize = Marshal.ReadInt32(pData);

    byte[] filebuf = new byte[uiSize];
    Marshal.Copy(pArrayLocation, filebuf, 0, uiSize);

    managedDataFile.GlobalUnlock(pData);
    managedDataFile.GlobalFree(fileData_ptr);
  }
}
catch
{
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900