pass in array of structures in P/Invoke Function

  • Thread starter Thread starter Rafiqah Abd.Rahman via DotNetMonster.com
  • Start date Start date
R

Rafiqah Abd.Rahman via DotNetMonster.com

I want to pass in an array of structures using a P/Invoke function. But
they currently give me a non-supported exception.How to solve this? Please
reply to me asap.I need the codes urgently!!

These are my codes.

These are at the wrapper class
==============================

<StructLayout(LayoutKind.Sequential, Size:=4)> _
Public Structure SSFT_ENGINE
'szEngine is a string
Public szEngine As StringPtr
'szSymbolicName is a string
Public szSymbolicName As StringPtr
Public eTechnology As SSFT_TECHNOLOGY
'szVersion is a string
Public szVersion As StringPtr
End Structure

<DllImport("rssoloapi.dll", EntryPoint:="ssft_ttsGetEngineList")> _
Public Shared Function ssft_ttsGetEngineList( _
ByRef hSpeech As SSFT_HSAFE, _
ByVal technology As SSFT_TECHNOLOGY, _
ByRef pEngines() As SSFT_ENGINE, _
ByRef numOfElements As Int32) As Integer

End Function

These codes are at the Form side
================================
SSFT_ERROR = ssft_ttsGetEngineList
(hSpeech,SSFT_TECHNOLOGY.SSFT_TECHNOLOGY_TTS, _
engines, Convert.ToInt32(NUM_ENGINES))
Label1.Text &= "Result of ssft_ttsGetEngineList :" & _
SSFT_ERROR.ToString & vbCrLf
 
The marshaller can only marshal a simple structure with blittable types
and it is impossible to pass array of structures as well as include into
structure another structure - fields of embedded structure must be
explicitly included instead of structure itself.

The workaround for this issue is to allocate a byte array (or use
LocalAlloc P/Invoke) with the size equals to onvert.ToInt32(NUM_ENGINES)
* size of your structure, pass this array instead of structure and
manually calculate the location of each field of structure in the array.

This links may help you:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfintrointerp.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/netcfadvinterop.asp

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
Hey thanks for the info.I will do and test out the codes.Hmm another
question that i wish to ask.I currently got an invalid handle whenever i
call P/Invoke.Why did i have it in the first place?Currently im developing
on 3rd party API.
 
Regarding using 3rd Party API, what is the best way that i can do to be
able to acess the functions inside it?Right now i'm using the pinvoke
method to wrap the dll files.Any suggestions?
 
If it's native (and it sounds like it is) and they vendor doesn't provide a
managed wrapper, then P/Invoke is the only option.

-Chris
 
Back
Top