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 / November 2005

Tip: Looking for answers? Try searching our database.

How to get a variant returned from a c++ function in vb.net?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Thomas W - 17 Nov 2005 09:39 GMT
Hello,

I'm trying to call a function from a C++ dll which returns a variant:

   VARIANT  DLLEXPORT ExecuteFunction (LPCSTR);

This function (which I can't change) executes a SQL query and returns a

single value - whatever the query asks for: integer, string, double
etc.

My declaration for that in VB.NET 2003 is

Private Declare Auto Function ExecuteFunction Lib "database.dll" ( _
           ByVal sSQL As Char) As Object

When I call that function, I'm getting the "PInvoke restriction: can
not return variants"-error.

How can I fix that? (sorry for cross posting in
microsoft.public.dotnet.languages.vb - no answers there, seems better
here)

Thanks Thomas
Lucvdv - 17 Nov 2005 10:35 GMT
> This function (which I can't change) executes a SQL query and returns a
> single value - whatever the query asks for: integer, string, double
> etc.

Is there a reason why you can't use .Net data objects and execute the query
directly from VB.Net code?  It sounds like ExecuteFunction does exactly the
same as the ExecuteScalar method of a .Net SqlCommand object.

Imports System.Data.SqlClient
[...]
Dim cn As New SqlConnection(ConnectionString)
cn.Open
Dim cmd As New SqlCommand("Your query", cn)
Dim x As Object = cmd.ExecuteScalar()

> Private Declare Auto Function ExecuteFunction Lib "database.dll" ( _
>             ByVal sSQL As Char) As Object

I could be wrong on some or all points, but I see more than one problem
here.

First, I think you'll have to pass the query differently: Char is a single
character, not a pointer to a string as the function expects.
I've never tried, but I think it will pass the ASCII value of the first
character of your query instead of a pointer to the string.

Second, but I'm really not sure here, I think "auto" assumes that the
function exists in ExecuteFunctionA and/or ExecuteFunctionW variations in
the DLL (as most API functions that take strings do), use either 'ansi' or
'unicode' depending on what the DLL expects.  Again, I've never tried, but
I think "auto" may append either an A or a W to the function name.

Third, I don't know if AsAny works on return values, but try this:

Private Declare Ansi Function ExecuteFunction Lib "database.dll" ( _
   ByVal sSQL As String) As <MarshalAs(UnmanagedType.AsAny)> Object

I think it will tell you that you can't use AsAny on a return value though.
Even if you can, the error message makes me expect you'll still get the
same error because returning variants is really not supported.
Thomas W - 18 Nov 2005 12:50 GMT
>> Is there a reason why you can't use .Net data objects and execute the query
>> directly from VB.Net code?

Yes: The database is confidential, access only via that dll. No direct
connection possible.

>> First, I think you'll have to pass the query differently: Char is a single
>> character,

You're right, it's "ByVal sSQL As String". Wrong example, sorry for
that

>> Second, but I'm really not sure here, I think "auto" assumes that the
>> function exists in ExecuteFunctionA and/or ExecuteFunctionW variations in
>> the DLL

Good idea, I changed to Ansi, but the problem remains the same

>> Third, I don't know if AsAny works on return values, but try this:
>> Private Declare Ansi Function ExecuteFunction Lib "database.dll" ( _
>>    ByVal sSQL As String) As <MarshalAs(UnmanagedType.AsAny)> Object

Interesting - now I'm getting a different error message, it's "Object
reference not set to an instance of an object" now.

Thanks so far - any idea?
Laurent - 18 Nov 2005 15:05 GMT
Hello,

Maybe, you can write a small wrapper that take a reference to the variant to
assign the result, instead of returning directly the variant. { I just
suppose that I can work, not sure }

>>> Is there a reason why you can't use .Net data objects and execute the
>>> query
[quoted text clipped - 25 lines]
>
> Thanks so far - any idea?

Rate this thread:







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.