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. calling a winapi function from managed code

calling a winapi function from managed code

Scheduled Pinned Locked Moved Managed C++/CLI
questioncomperformancehelp
2 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.
  • D Offline
    D Offline
    david sturzenegger
    wrote on last edited by
    #1

    hi, i have a problem when calling from the managed code the function capGetDriverDescription( WORD wDriverIndex, LPTSTR lpszName, INT cbName, LPTSTR lpszVer, INT cbVer ); from avicap32.dll. the function is described in detail in http://msdn.microsoft.com/en-us/library/dd756909%28VS.85%29.aspx[^]. Basically it writes the name and description of videoinput device number wDriverIndex to the passed strings lpszName and lpszVer which have length cbName and cbVer respectively (better put, to the corresponding memory). the relevant code is the following ----------------------------------------- [DllImport("avicap32.dll")] extern "C" bool capGetDriverDescription(UInt16, String^, int, String^, int); ..... int i_nameLen = 100; int i_verLen = 100; String^ s_name = gcnew String('x',i_verLen); String^ s_ver = gcnew String('x',i_verLen); result = capGetDriverDescription(x, s_name, i_nameLen, s_ver, i_verLen); ------------------------------------------ the function returns for x=0 true and for x=1 false which is expected since only one webcam is attached to my computer. but the returned strings s_name and s_ver are unchanged. probably managed and unmanaged code dont use the same memory which makes it somewhat difficult to pass pointers.... what is the alternative? btw, its my first time calling unmanaged code from managed one, so probably the answer will be quite obvious. thanks in advance! david ps: is there a smarter way to allocate memory to the strings? (probably yes...)

    G 1 Reply Last reply
    0
    • D david sturzenegger

      hi, i have a problem when calling from the managed code the function capGetDriverDescription( WORD wDriverIndex, LPTSTR lpszName, INT cbName, LPTSTR lpszVer, INT cbVer ); from avicap32.dll. the function is described in detail in http://msdn.microsoft.com/en-us/library/dd756909%28VS.85%29.aspx[^]. Basically it writes the name and description of videoinput device number wDriverIndex to the passed strings lpszName and lpszVer which have length cbName and cbVer respectively (better put, to the corresponding memory). the relevant code is the following ----------------------------------------- [DllImport("avicap32.dll")] extern "C" bool capGetDriverDescription(UInt16, String^, int, String^, int); ..... int i_nameLen = 100; int i_verLen = 100; String^ s_name = gcnew String('x',i_verLen); String^ s_ver = gcnew String('x',i_verLen); result = capGetDriverDescription(x, s_name, i_nameLen, s_ver, i_verLen); ------------------------------------------ the function returns for x=0 true and for x=1 false which is expected since only one webcam is attached to my computer. but the returned strings s_name and s_ver are unchanged. probably managed and unmanaged code dont use the same memory which makes it somewhat difficult to pass pointers.... what is the alternative? btw, its my first time calling unmanaged code from managed one, so probably the answer will be quite obvious. thanks in advance! david ps: is there a smarter way to allocate memory to the strings? (probably yes...)

      G Offline
      G Offline
      Ghydo
      wrote on last edited by
      #2

      To use a string as an output parameter you should use the StringBuilder class instead of String.

      [DllImport("avicap32.dll"),CharSet=CharSet.Unicode]
      extern "C" bool capGetDriverDescription(UInt16, StringBuilder^, int, StringBuilder^, int);

      But in C++/CLI you can directly call the native C function without use P/Invoke: you can mix managed and unmanaged code. Hope it helps! :)

      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