Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PInvoke error in VB.NET 2005

Posted on 2006-11-23
4
Medium Priority
?
886 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 5

Expert Comment

by:DEEPESH
ID: 18002328
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 2000 total points
ID: 18002605
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
ID: 18006358
Any comment on it vj_mj
0
 

Author Comment

by:vj_mi
ID: 18007492
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

664 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