Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. How to get an Array allocated in Unmanamed code

How to get an Array allocated in Unmanamed code

Scheduled Pinned Locked Moved Managed C++/CLI
c++data-structuresjsonhelptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hasansheik
    wrote on last edited by
    #1

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

    U 1 Reply Last reply
    0
    • H hasansheik

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

      U Offline
      U Offline
      User 929285
      wrote on last edited by
      #2

      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 importing String *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?

      H 1 Reply Last reply
      0
      • U User 929285

        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 importing String *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?

        H Offline
        H Offline
        hasansheik
        wrote on last edited by
        #3

        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.

        U 1 Reply Last reply
        0
        • H hasansheik

          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.

          U Offline
          U Offline
          User 929285
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups