calling a winapi function from managed code
-
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...)
-
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...)
To use a string as an output parameter you should use the
StringBuilder
class instead ofString
.[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! :)