Solved

PInvoke error in VB.NET 2005

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 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