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 / December 2004

Tip: Looking for answers? Try searching our database.

Best way to Marshal a string for a VB wrapper

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
M K - 14 Dec 2004 17:05 GMT
I am trying to wrap this function:
int KPDCIteratorFind(KPDCIteratorRef inIteratorRef, const char *inName,
KPDCOpaqueRef *outfoundItemRef);

I have everything except inName working. inName should be 'A pointer to a
null-terminated, 8-bit ASCII character string. This pointer must not be NULL.'

I tried creating a wrapper using 'inName As StringBuilder'. But it doesn't
seem to work. Any ideas?

Here's the code that calls my wrapper.
 Dim attrName As New System.Text.StringBuilder(255)
   attrName.Append("saveEnable")
   myStatus = myPro4.KIteratorFind(Me.myPropIter, attrName, Me.myPropRef)

And in 'doesn't work', the function reports it can't find that 'attrName',
but it should. So I'm wondering if the marshalling isn't going right.
Eric Carlson - 14 Dec 2004 18:09 GMT
Try this:

[MarshalAs(UnmanagedType.LPStr)] ref string inName

> I am trying to wrap this function:
> int KPDCIteratorFind(KPDCIteratorRef inIteratorRef, const char *inName,
[quoted text clipped - 13 lines]
> And in 'doesn't work', the function reports it can't find that 'attrName',
> but it should. So I'm wondering if the marshalling isn't going right.
M K - 14 Dec 2004 19:21 GMT
In VB that would look like this. Thank you. That would normally be fine. Now
I have to figure out why the function still doesn't like it.

> Try this:
>
[quoted text clipped - 17 lines]
> > And in 'doesn't work', the function reports it can't find that 'attrName',
> > but it should. So I'm wondering if the marshalling isn't going right.
Eric Carlson - 14 Dec 2004 19:37 GMT
Can you give a little more detail... i.e. from what you've said it sounds
like you have some VB code that you are trying to call from .Net ?  Are you
importing this function into .Net with dllimport?

Or do i have this all wrong?? :)
M K - 14 Dec 2004 20:43 GMT
It's an unmanaged C++ DLL that I'm trying to use in my managed VB app. I have
documentation for the DLL, but it's all C++, not my first language.

What you gave me helped, with something else. The unmanaged function I
posted here about should take a string and find that property in an iterator.
I can walk the iterator and find the property, but this unmanaged function
doesn't find it. I think I will manually search the iterator. (I believe
that's what the unmanaged function is doing anyways.)

But I used MarshalAs UnmanagedType.AsAny for another function that needed to
take a boolean.

> Can you give a little more detail... i.e. from what you've said it sounds
> like you have some VB code that you are trying to call from .Net ?  Are you
> importing this function into .Net with dllimport?
>
> Or do i have this all wrong?? :)
Eric Carlson - 14 Dec 2004 21:45 GMT
So, where are you at right now...

Does your function execute without error?
Does it just return and tell you that it couldn't find the item?
Are you absolutely sure that you are marshalling all of the parameters
correctly? (i.e. the inName ? )

A lot of times functions can execute without error, though the data is not
being marshalled correctly so you get bad results.

Can you post your complete vb.net "KPDCIteratorFind" function here?
M K - 14 Dec 2004 22:15 GMT
Yes, I believe that is it. I do not believe the inName parameter is
marshalling correctly.

I don't have the KPDCIteratorFind function, as it is in unmanaged DLL that I
do not have the source code too.

Here is what the documentation gives me for this function:
int KPDCIteratorFind(KPDCIteratorRef inIteratorRef, const char *inName,
KPDCOpaqueRef *outfoundItemRef);

The function executes without error, it simple returns an int value other
than 0. According to the documentation, 0 means the function fired okay. The
int I get (201) means that it could not find the item.

I know the item is there, because I can see it walking the iterator.

> So, where are you at right now...
>
[quoted text clipped - 7 lines]
>
> Can you post your complete vb.net "KPDCIteratorFind" function here?
Eric Carlson - 15 Dec 2004 16:25 GMT
Ok, thank for that info... now post your vb.net import for this function, and
tell me the different ways that you have tried to marshal your inName string,
then i can hopefully give you some suggestions.
M K - 15 Dec 2004 17:13 GMT
Declare Auto Function KIteratorFind Lib "DCSPro4SLR.dll" Alias
"KPDCIteratorFind" (ByVal inIteratorRef As IntPtr,
<MarshalAs(UnmanagedType.LPStr)> ByRef inName As String, ByRef
outFoundItemRef As IntPtr) As Integer

I've also tried:
Declare Auto Function KIteratorFind Lib "DCSPro4SLR.dll" Alias
"KPDCIteratorFind" (ByVal inIteratorRef As IntPtr, ByRef inName As
StringBuilder, ByRef outFoundItemRef As IntPtr) As Integer

Thanks

> Ok, thank for that info... now post your vb.net import for this function, and
> tell me the different ways that you have tried to marshal your inName string,
> then i can hopefully give you some suggestions.
Eric Carlson - 15 Dec 2004 20:13 GMT
Have you tried sending the inName as ByVal ?

"KPDCIteratorFind" (ByVal inIteratorRef As IntPtr,
<MarshalAs(UnmanagedType.LPStr)> ByVal inName As String, ByRef
outFoundItemRef As IntPtr) As Integer
M K - 15 Dec 2004 21:21 GMT
Sorry, you are correct. I thought I had, but I went through one more time.
And it worked. Thank you.

> Have you tried sending the inName as ByVal ?
>
> "KPDCIteratorFind" (ByVal inIteratorRef As IntPtr,
> <MarshalAs(UnmanagedType.LPStr)> ByVal inName As String, ByRef
> outFoundItemRef As IntPtr) As Integer

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.