Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2006

Tip: Looking for answers? Try searching our database.

How To: C++ DLL function returns Structure Array to VB .NET

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lov4mu6@gmail.com - 16 Sep 2006 01:24 GMT
I have a DLL that I've compiled in Visual C++ which I'm referencing in
VB .NET

The function in the DLL returns a simple structure array. I need to be
able to capture this structure array in VB .NET.

C++ code:
struct NotesInfo
{
      int Note;
      int StartTime;
      int EndTime;
};

NotesInfo mytest(char *fHMM, char *fWaveUserQuery)
{
      NotesInfo *Score;
      Score = new NotesInfo[2];

      Score[0].StartTime = 0;
      Score[0].EndTime = 1;
      Score[0].Note = 60;

      Score[1].StartTime = 2;
      Score[1].EndTime = 3;
      Score[1].Note = 70;

      return *Score;
}

above code compiles into DLL.

In VB.NET, I have coded the same structure:
Structure ScoreData
  Dim Note AS Integer
  Dim StartTime AS Integer
  Dim EndTime AS Integer
End Structure

This is how I reference the DLL in VB .NET:
Private Declare Function mytest Lib "C:\MyLib.dll" (ByVal FileTrainData
As String, ByVal FileWaveUsrQuery AS String) AS IntPtr

I'm not sure if I need to declare mytest function as IntPtr or as
ScoreData? Either way, I need to be able to capture the structure array
output from the DLL and assign it to the structure in VB .NET

Any suggestions are greatly appreciated!
Vlad.
Mattias Sjögren - 16 Sep 2006 12:36 GMT
>NotesInfo mytest(char *fHMM, char *fWaveUserQuery)
>{
[quoted text clipped - 11 lines]
>       return *Score;
>}

This doesn't return an array, it returns a single NotesInfo instance.
Plus it leaks memory since you don't have any way of releasing the
array memory after the function returns.

I suggest you rewrite it to return the array via output parameters.

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

lov4mu6@gmail.com - 16 Sep 2006 21:09 GMT
Thank you! Would you possibly give an example of how that would be
done?

Vlad.

> >NotesInfo mytest(char *fHMM, char *fWaveUserQuery)
> >{
[quoted text clipped - 24 lines]
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
Mattias Sjögren - 25 Sep 2006 21:01 GMT
>Thank you! Would you possibly give an example of how that would be
>done?

Something like this

bool mytest(NotesInfo *Score, unsigned int count)
{
 if (!Score || count < 2) return false;

 Score[0].StartTime = 0;
 Score[0].EndTime = 1;
 Score[0].Note = 60;

 Score[1].StartTime = 2;
 Score[1].EndTime = 3;
 Score[1].Note = 70;

 return true;

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.