Solved

PInvoke error in VB.NET 2005

Posted on 2006-11-23
4
878 Views
Last Modified: 2012-05-05
Hello,
In VB.NET 2003, we used GetWindowsDirectory to get the windows directory name and we check if our app ini file exist. Now when I migrate to VB.NET 2005, I get following error:
A call to PInvoke function 'Demo!Demo.ClsGeneral::GetWindowsDirectory' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Below is the code in Form:
Dim objGeneral As New ClsGeneral(gstrCultureDateInfo)
If objGeneral.CheckForINIFile("Test.INI", gstrINIFilePath) = False Then
                'Display message
                MessageBox.Show("INI file does not exist. Contact System Administrator.", MSG_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Stop)
                Exit Sub
End If


Below is the code in class file that is compiled as dll and reference set in the above project:
    Public Function CheckForINIFile(ByVal strINIFileName As String, Optional ByRef strINIFilePath As String = "") As Boolean
        Dim strChkFile As String
        Dim intRetValue As Integer
        Dim strFileName As String

        Try
            'Gets the Windows system directory
            strFileName = Space$(1024)
            intRetValue = GetWindowsDirectory(strFileName, 255)
            strFileName = Mid(strFileName, 1, intRetValue)

            'Checks if the INI files exists. If it does not exists then exit the program
            strChkFile = Dir(strFileName & "\" & strINIFileName)

            If strINIFileName.ToUpper <> strChkFile.ToUpper Then
                'If INI does not exist ; return false
                CheckForINIFile = False
                Exit Function
            End If

            'Get INI File Path
            INIFilePath = strFileName & "\" & strINIFileName

            'Set the INI File Path that would be returned due to ByRef
            strINIFilePath = strFileName & "\" & strINIFileName

            CheckForINIFile = True

        Catch
            CheckForINIFile = False
            Exit Function

        Finally

        End Try
    End Function


Below is my declaration in class file:
    Private Declare Unicode Function GetWindowsDirectory Lib "kernel32" _
            Alias "GetWindowsDirectoryW" (ByVal lpBuffer As String, _
            ByVal iLen As Long) As Int32


0
Comment
Question by:vj_mi
  • 3
4 Comments
 
LVL 5

Expert Comment

by:DEEPESH
Comment Utility
this may have happened because of some missing patches.
But instead why dont u use

for seacrhing exisitng directory we have method in vb.net 2005
System.IO.Directory.Exists

and for file existence
system.IO.File.Exists
0
 
LVL 5

Accepted Solution

by:
DEEPESH earned 500 total points
Comment Utility
Sorry i am mistaken pinvoke error occurring because of long parameter in your api
nothing to do with patches.

 change your api as
Private Declare Unicode Function GetWindowsDirectory Lib "kernel32" _
            Alias "GetWindowsDirectoryW" (ByVal lpBuffer As String, _
            ByVal iLen As int32) As Int32

long has been changed to  ByVal iLen As int32

 
0
 
LVL 5

Expert Comment

by:DEEPESH
Comment Utility
Any comment on it vj_mj
0
 

Author Comment

by:vj_mi
Comment Utility
Sorry Deepesh .... I was little busy with migration from 2003 to 2005 as it produces good number of warnings and I had to fix them. Your solution works.... thanks!

Regards,
MI
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now