Error: "managed PInvoke signature does not match the unmanaged target signature."

Dre__

Newcomer
Joined
Mar 12, 2010
Messages
4
Error: "managed PInvoke signature does not match the unmanaged target signature."

I have this:

Code:
 '----Module 1----

Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long

     Function GetStuff(ByVal appname As String, ByVal KEY As String) As String
        Dim sFile As String
        Dim sDefault As String
        Dim lSize As Integer
        Dim l As Long
        Dim sUser As String

        sUser = Space$(128)
        lSize = Len(sUser)
        sFile = Application.StartupPath & "\Config.ini"
        sDefault = ""
        l = GetPrivateProfileString(appname, KEY, sDefault, sUser, lSize, sFile)
        sUser = Mid(sUser, 1, InStr(sUser, Chr(0)) - 1)
        GetStuff = sUser
    End Function

Code:
----Form1----
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GetStuff("Configurations", "Test")
    End Sub


When I click "Button1", I get this error:
A call to PInvoke function 'MyProgram!WindowsApplication1.Module1::GetPrivateProfileString' 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.

I read throgh the MSDN that the error box suggests, but I still have no idea how to fix the problem.

Does anyone have any suggestions?
 
Re: Error: "managed PInvoke signature does not match the unmanaged target signature."

Did you get the declaration from a VB6 source? Under .Net you might want to try changing the Longs to Integers and see if that helps.

Then again if you are using .Net you might want to look at using a more .Net based solution such as the application settings as a way to store configuration information.

Storing the config in the same folder as the application will cause problems for non admin users on xp or later due to permissions on the program files folder.
 
Re: Error: "managed PInvoke signature does not match the unmanaged target signature."

Ye I copied the code from my vb6 application. I'll try to find a .net solution, but to save some time, do you have any suggestions or tutorials on how to do this?

First I'll try the Long-Integer switch and if doesn't work, I'll look for a .net solution to do this.
 
Re: Error: "managed PInvoke signature does not match the unmanaged target signature."

If you are using VS 2005 or later simply bring up the project properties and there should be a tab for settings on the left. You can then provide settings with a name, type and default value while specifying if they are stored at the user or application level.

At run time you can then refer to these settings via the My.Settings object.

I am also not sure what the support for the various PrivateProfileString functions is under 64 bit versions of windows, it was only carried over to the 32 bit versions for compatability with 16 bit windows, you may find 64 bit apps are unable to use this call.
 
Re: Error: "managed PInvoke signature does not match the unmanaged target signature."

Thanks for the help guys. This seems to work:

Code:
Module mdlnewWriteGetIni
    Public Class IniFile
        ' API functions
        Private Declare Ansi Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
          ByVal lpKeyName As String, ByVal lpDefault As String, _
          ByVal lpReturnedString As System.Text.StringBuilder, _
          ByVal nSize As Integer, ByVal lpFileName As String) _
          As Integer
        Private Declare Ansi Function WritePrivateProfileString _
          Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
          (ByVal lpApplicationName As String, _
          ByVal lpKeyName As String, ByVal lpString As String, _
          ByVal lpFileName As String) As Integer
        Private Declare Ansi Function GetPrivateProfileInt _
          Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
          (ByVal lpApplicationName As String, _
          ByVal lpKeyName As String, ByVal nDefault As Integer, _
          ByVal lpFileName As String) As Integer
        Private Declare Ansi Function FlushPrivateProfileString _
          Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
          (ByVal lpApplicationName As Integer, _
          ByVal lpKeyName As Integer, ByVal lpString As Integer, _
          ByVal lpFileName As String) As Integer
        Dim strFilename As String

        ' Constructor, accepting a filename
        Public Sub New(ByVal Filename As String)
            strFilename = Filename
        End Sub

        ' Read-only filename property
        ReadOnly Property FileName() As String
            Get
                Return strFilename
            End Get
        End Property

        Public Function GetString(ByVal Section As String, ByVal Key As String, ByVal [Default] As String) As String
            ' Returns a string from your INI file
            Dim intCharCount As Integer
            Dim objResult As New System.Text.StringBuilder(256)
            intCharCount = GetPrivateProfileString(Section, Key, [Default], objResult, objResult.Capacity, strFilename)
            If intCharCount > 0 Then GetString = Left(objResult.ToString, intCharCount)
        End Function

        Public Function GetInteger(ByVal Section As String, _
          ByVal Key As String, ByVal [Default] As Integer) As Integer
            ' Returns an integer from your INI file
            Return GetPrivateProfileInt(Section, Key, _
               [Default], strFilename)
        End Function

        Public Function GetBoolean(ByVal Section As String, _
          ByVal Key As String, ByVal [Default] As Boolean) As Boolean
            ' Returns a boolean from your INI file
            Return (GetPrivateProfileInt(Section, Key, _
               CInt([Default]), strFilename) = 1)
        End Function

        Public Sub WriteString(ByVal Section As String, _
          ByVal Key As String, ByVal Value As String)
            ' Writes a string to your INI file
            WritePrivateProfileString(Section, Key, Value, strFilename)
            Flush()
        End Sub

        Public Sub WriteInteger(ByVal Section As String, _
          ByVal Key As String, ByVal Value As Integer)
            ' Writes an integer to your INI file
            WriteString(Section, Key, CStr(Value))
            Flush()
        End Sub

        Public Sub WriteBoolean(ByVal Section As String, _
          ByVal Key As String, ByVal Value As Boolean)
            ' Writes a boolean to your INI file
            WriteString(Section, Key, CStr(CInt(Value)))
            Flush()
        End Sub

        Private Sub Flush()
            ' Stores all the cached changes to your INI file
            FlushPrivateProfileString(0, 0, 0, strFilename)
        End Sub
    End Class
End Module

Code:
'-Form1-
Dim objIniFile As New IniFile(Application.StartupPath & "/Data.ini")

    Private Sub cmdGetINI_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetINI.Click
        Dim strData As String
        strData = objIniFile.GetString("Settings", "ClockTime", "(None)")
        MsgBox(strData)
    End Sub

    Private Sub cmdWriteINI_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdWriteINI.Click
        objIniFile.WriteString("Settings", "ClockTime", "12:59")
    End Sub
 
Back
Top