Results 1 to 7 of 7

Thread: [RESOLVED] "as any" problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2001
    Posts
    205

    Resolved [RESOLVED] "as any" problem

    I'm new to .net and just upgraded a vb6 project. I'm fixing upgrade problems but ran into an issue I can't figure out. I know that "as any" isn't allowed any more. But I have a function declaration that passes a structure and I can't figure out how to declare it for .net. Here's the function. LicInfo is a structure. Any ideas?

    Public Declare Function GMW_GetLicenseInfo Lib "gm7s32.dll" (ByRef LicInfo As Any) As Integer

  2. #2
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: "as any" problem

    I normally change it to int32, try that. Works for me alot

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: "as any" problem

    The best way to do external function declarations in .NET is to import the System.Runtime.Interop namespace and use <DllImport> on the function signature. Leave the function body blank (you need an End Function too). And use <MarshalAs...> on troublesome parameters, you will find a lot of options that allow you to control exactly how the function is called.

  4. #4
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: "as any" problem

    If it is a structure, then pass an instance ByRef. You may need to initialise members that are arrays, strings or further structures. It is very likely it won't work without some custom marshalling attributes applied, and you might need to set a size member of the structure to the correct value (the size, in bytes of the structure - which is not always easy to determine).

    Integer might work as the c funciton is expecting a pointer to a structure, and pointers and integers are both 4 byte values. The returned int then is really an IntPtr to a structure, but it probably wouldn't work without sending off a blob of memory holding an initialised structure...

    If you have the declaration of the structure (the c one is best), then I could give a code sample with a best guess...

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2001
    Posts
    205

    Re: "as any" problem

    This is the c code for the structure and the function:

    VB Code:
    1. licensing structure passed to GMW_GetLicenseInfo
    2. typedef struct
    3. {
    4. char szLicensee[60]; // licensee name
    5. char szLicNo[20]; // master serial number
    6. char szSiteName[20]; // undocked site name
    7. long iLicUsers; // licensed users
    8. long iSQLUsers; // licensed SQL users
    9. long iGSSites; // license GoldSync sites
    10. long isDemo; // is demo install
    11. long isServerLic; // is primary license ('D' or 'E')
    12. long isRemoteLic; // is remote license ('U' or 'S')
    13. long isUSALicense; // is USA license (1=US,128/32
    14. // bit, 0=nonUS, 32-bit only)
    15. long iDLLVersion; // the DLL version (400822)
    16. long iReserved1;
    17. long iReserved2;
    18. long szReserved[100];
    19. } GMW_LicInfo;
    20.  
    21. int _stdcall GMW_GetLicenseInfo( GMW_LicInfo *pLic );

  6. #6
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: "as any" problem

    Hmm, here's an attempt. It's very difficult without being able to test it.

    VB Code:
    1. <DllImport("gm7s32.dll")> _
    2.     Private Shared Function GMW_GetLicenseInfo(ByRef licenseInfo As GMW_LicInfo) As Integer
    3.     End Function
    4.  
    5.     <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
    6.     Private Structure GMW_LicInfo
    7.         ' msdn says a c CHAR is an 8 bit ansi character.
    8.         ' we can marshal it as a string if we initialise it first.
    9.         ' see topic "default marshaling for strings" in the library.
    10.         ''' <summary>licensee name</summary>
    11.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=60)> _
    12.         Public szLicensee As String
    13.         ''' <summary>master serial number</summary>
    14.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=20)> _
    15.         Public szLicNo As String
    16.         ''' <summary>undocked site name</summary>
    17.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=20)> _
    18.         Public szSiteName As String
    19.         ' msdn says a c LONG is a 32-bit signed integer, so use int32
    20.         ''' <summary>licensed users</summary>
    21.         Public iLicUsers As Integer
    22.         ''' <summary>licensed SQL users</summary>
    23.         Public iSQLUsers As Integer
    24.         ''' <summary>license GoldSync sites</summary>
    25.         Public iGSSites As Integer
    26.         '''<summary>is demo install</summary>
    27.         Public isDemo As Integer
    28.         '''<summary>is primary license ('D' or 'E')</summary>
    29.         Public isServerLic As Integer
    30.         ''' <summary>is remote license ('U' or 'S')</summary>
    31.         Public isRemoteLic As Integer
    32.         ''' <summary>is USA license (1=US,128/32bit, 0=nonUS, 32-bit only.</summary>
    33.         Public isUSALicense As Integer
    34.         ''' <summary>the DLL version (400822)</summary>
    35.         Public iDLLVersion As Integer
    36.         Public iReserved1 As Integer
    37.         Public iReserved2 As Integer
    38.         ' See topic 'Default marshaling for arrays' in the library
    39.         <MarshalAs(UnmanagedType.ByValArray, ArraySubType:=UnmanagedType.I4, SizeConst:=100)> _
    40.         Public szReserved() As Integer
    41.  
    42.         Public Sub Initialize()
    43.             ' fill the strings with dummy data
    44.             Me.szLicensee = New String("*"c, 60)
    45.             Me.szLicNo = New String("*"c, 20)
    46.             Me.szSiteName = New String("*"c, 20)
    47.             Me.szReserved = New Int32(99) {}
    48.             szReserved.Initialize()
    49.         End Sub
    50.     End Structure
    51.  
    52.  
    53.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    54.  
    55.         Dim licInfo As New GMW_LicInfo
    56.         licInfo.Initialize()
    57.         ' call
    58.         Dim result As Integer = GMW_GetLicenseInfo(licInfo)
    59.         ' hopefully licinfo is now filled in...
    60.  
    61.     End Sub
    Last edited by jo0ls; Jul 15th, 2006 at 11:57 AM.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2001
    Posts
    205

    Re: "as any" problem

    Simple solution. I changed the "any" to the name of the structure.

    Public Declare Function GMW_GetLicenseInfo Lib "gm7s32.dll" (ByRef LicInfo As GMW_LicInfo) As Integer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width