Solved

PInvoke error in VB.NET 2005

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net 1 month apart 11 38
vb.net class 3 28
How to limit User Input 2 33
UDP Data Capture Thread Issue 7 30
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…

735 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