Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: Grok on December 07, 2005, 08:23 AM

Title: MarshallAsAttribute
Post by: Grok on December 07, 2005, 08:23 AM
As Any ... used to be valid in VB6, not valid in VB.NET .. documentation says to use MarshallAsAttribute to fix this.

Declare Function KGI_CNMInitServices Lib "wkgi32.dll" (ByVal bBlocked As Long, <MarshallAsAttribute(UnmanagedType.AsAny)> ByVal pNode As Object, ByVal perr As Long) As Boolean

However, "Type MarshallAsAttribute" is not defined" is the error I get.

Imports System.Attribute does not help, still not defined.  The declare is in a standard module, Module1.vb.  What do I need to import so that MarshallAsAttribute is defined?

Bigger question, how are these situations (C DLLs, memory blocks, pointers) handled in .NET?
Title: Re: MarshallAsAttribute
Post by: MyndFyre on December 07, 2005, 10:04 AM
You need to Imports System.Runtime.InteropServices.MarshalAsAttribute.
Title: Re: MarshallAsAttribute
Post by: Grok on December 07, 2005, 10:18 AM
Doesn't work.


Imports System.Runtime.InteropServices.MarshalAsAttribute

Module VBKGI
    '***********************************************
    '           GENERAL FUNCTIONS
    '***********************************************

    Declare Function KGI_CNMInitServices Lib "wkgi32.dll" (ByVal bBlocked As Long, <MarshallAsAttribute(UnmanagedType.AsAny)> ByVal pNode As Object, ByVal perr As Long) As Boolean
End Module


Still underlines MarshallAsAttribute and says it is not defined.
Title: Re: MarshallAsAttribute
Post by: MyndFyre on December 07, 2005, 10:39 AM
Because Marshal is spelled with one L.

You should use:

Imports System.Runtime.InteropServices

to include all the types in that namespace, because your next compiler error will involve the undefined type "UnmanagedType".

Also, depending on the use of pNode, you should probably be able to use it as an IntPtr, or as the structure that the function will be called with.  It's very odd to see you wanting to pass a managed object out as data into a function.

Finally, you should consider not using a Declare, but instead using the DllImport attribute:

<DllImport("wkgi32.dll")>_
Public Shared Function KGI_CNMInitServices(ByVal bBlocked As Boolean, _
<MarshalAs(UnmanagedType.AsAny)> ByVal pNode As Object, _
ByVal pErr As Long) As Boolean


Want to contract me to do this work for you Grok?  :P
Title: Re: MarshallAsAttribute
Post by: Grok on December 07, 2005, 03:02 PM
Quote from: MyndFyre on December 07, 2005, 10:39 AMWant to contract me to do this work for you Grok?  :P

Why do you ask?
Title: Re: MarshallAsAttribute
Post by: Grok on December 07, 2005, 03:41 PM
Found what I need in MSDN topic:  Consuming Unmanaged DLL Functions
Title: Re: MarshallAsAttribute
Post by: MyndFyre on December 07, 2005, 04:54 PM
Quote from: Grok on December 07, 2005, 03:02 PM
Quote from: MyndFyre on December 07, 2005, 10:39 AMWant to contract me to do this work for you Grok?  :P

Why do you ask?
Because I wouldn't mind contract work.  :)