How to get an Array allocated in Unmanamed code
-
hi all, here is my problem i want to call a api(ReadFile) fucnction which return the byte array . how i have to declare and pass variables from managed c++.
-
hi all, here is my problem i want to call a api(ReadFile) fucnction which return the byte array . how i have to declare and pass variables from managed c++.
There are two ways you can call ReadFile: First way: import the function:
[DllImport(S"Kernel32.dll", EntryPoint="ReadFile", CharSet=CharSet::Auto)] extern "C" Boolean ReadFileNative(IntPtr hFile,StringBuilder *lpBuffer, UInt32 nNumberOfBytesToRead, UInt32 *lpNumberOfBytesRead, Int32);
Here's a function that returns text from a file:String *GetFileTextWithImport(String *fileName){ FileStream *fs=new FileStream(fileName,FileMode::Open); //Wrapping a handle with HandleRef guarantees that //the managed object is not garbage collected //until the platform invoke call completes. HandleRef hRef=HandleRef(fs,fs->Handle); StringBuilder *sbText=new StringBuilder(fs->Length); UInt32 uiToRead=0; ReadFileNative(hRef.Handle,sbText,fs->Length,&uiToRead,0); fs->Dispose(); return sbText->ToString(); }
Second Way: just call ReadFile without importingString *GetFileText(String *fileName){ FileStream *fs=new FileStream(fileName,FileMode::Open); HandleRef hRef=HandleRef(fs,fs->Handle); IntPtr ptrText=Marshal::AllocHGlobal(fs->Length); DWORD uiToRead=fs->Length, uiRead=0; ReadFile((HANDLE)hRef.Handle,ptrText.ToPointer(),uiToRead,&uiRead,NULL); String *strRet=Marshal::PtrToStringAnsi(ptrText); Marshal::FreeHGlobal(ptrText); fs->Dispose(); return strRet;
P.S. By the way, why not use the System.IO.StreamReader? -
There are two ways you can call ReadFile: First way: import the function:
[DllImport(S"Kernel32.dll", EntryPoint="ReadFile", CharSet=CharSet::Auto)] extern "C" Boolean ReadFileNative(IntPtr hFile,StringBuilder *lpBuffer, UInt32 nNumberOfBytesToRead, UInt32 *lpNumberOfBytesRead, Int32);
Here's a function that returns text from a file:String *GetFileTextWithImport(String *fileName){ FileStream *fs=new FileStream(fileName,FileMode::Open); //Wrapping a handle with HandleRef guarantees that //the managed object is not garbage collected //until the platform invoke call completes. HandleRef hRef=HandleRef(fs,fs->Handle); StringBuilder *sbText=new StringBuilder(fs->Length); UInt32 uiToRead=0; ReadFileNative(hRef.Handle,sbText,fs->Length,&uiToRead,0); fs->Dispose(); return sbText->ToString(); }
Second Way: just call ReadFile without importingString *GetFileText(String *fileName){ FileStream *fs=new FileStream(fileName,FileMode::Open); HandleRef hRef=HandleRef(fs,fs->Handle); IntPtr ptrText=Marshal::AllocHGlobal(fs->Length); DWORD uiToRead=fs->Length, uiRead=0; ReadFile((HANDLE)hRef.Handle,ptrText.ToPointer(),uiToRead,&uiRead,NULL); String *strRet=Marshal::PtrToStringAnsi(ptrText); Marshal::FreeHGlobal(ptrText); fs->Dispose(); return strRet;
P.S. By the way, why not use the System.IO.StreamReader?thanx, _Ael in my case i can't use StreamReader b'cos , I want Asyncronous function call. I am using ReadFile for RS232 port reading. If we can use StreamReader or other managed call for asyncronous calls, then please let me know . how can i do the same.
-
thanx, _Ael in my case i can't use StreamReader b'cos , I want Asyncronous function call. I am using ReadFile for RS232 port reading. If we can use StreamReader or other managed call for asyncronous calls, then please let me know . how can i do the same.
If we can use StreamReader or other managed call for asyncronous calls, then please let me know . how can i do the same. FileStream::BeginRead FileStream::EndRead