Solved

PInvoke error in VB.NET 2005

Posted on 2006-11-23
4
880 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
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

947 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

20 Experts available now in Live!

Get 1:1 Help Now