DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: API Problems

  1. #1
    Join Date
    Sep 2005

    Unhappy API Problems

    Hi All,

    I have a couple of problems trying to use the API in .NET

    I have declared the following API:

    <DllImport("Kernel32", BestFitMapping:=False, CallingConvention:=CallingConvention.StdCall, _
    CharSet:=CharSet.Unicode, EntryPoint:="GetPrivateProfileStringA", ExactSpelling:=True)> _
    Public Function GetPrivateProfileString(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
    End Function

    When I run my code and call the above function. Where I have checked the filename/path is correct and similarly the section and key, I am returned an empty string in lpReturnString??

    The next one relates to ODBC.

    <DllImport("odbc32.dll", BestFitMapping:=False, CallingConvention:=CallingConvention.StdCall, _
    CharSet:=CharSet.Unicode, EntryPoint:="SQLAllocEnv", ExactSpelling:=True)> _
    Public Shared Function SQLAllocEnv(ByVal phenv As Integer) As Short
    'leave this function empty
    End Function

    again when I call this, I have -1 being returned and I do not know why.

    Any help would be appreciated.

  2. #2
    Join Date
    Oct 2005
    Not sure exactly whats going on, I am C# more than VB but have done a lot of this kind of stuff.

    I notice that all args to the GetPrivateProfileString are "ByVal", doesnt this actually preclude any possibility of the caller modifying them?

    Also make sure you understand the need for StringBuilder when calling Win32 functions, this is the only way to ensure that the called code actually gets passed a buffer of sufficient length.

    Passing a "String" for an arg that the caller will modify is bad.

    If you pass a String that contains "hello" then (in C/C++) the called code gets a pointer to a buffer that is just about large enough to hold five characters.

    If that code then modifies the string buffer to set it to "goodbye from me" then it will overwrite memory that it doesnt own and cause runtime problems/crashes.

    StringBuilder lets you create a string object but one that has a fixed buffer whose size you determine when you craete it: StringBuilder data = new StringBuilder(128);

    In this example "data" is to all intents and purposes a "String" but the buffer is fixed length of 128 bytes, so if you then code: data = "hello" and call tthe same func, and that func writes "goodbye from me" no harm is done, because the buffer is 128 bytes long.

    This is possibly something you need tp look into, but as I say I am not too expert on VB or VB.NET.


  3. #3
    Join Date
    Dec 2003
    For GetPrivateProfileString you probably need to properly initialize a string parameter. It would help to see the code you used when calling the API function call.

    The following code works for me when using the SQLAllocEnv API function call:

        Private Declare Function SQLAllocEnv Lib "ODBC32.DLL" (ByRef env As Integer) As Short
        Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv As Integer, ByVal fDirection As Short, ByVal szDSN As String, ByVal cbDSNMax As Short, ByRef pcbDSN As Short, ByVal szDescription As String, ByVal cbDescriptionMax As Short, ByRef pcbDescription As Short) As Short
        Const SQL_FETCH_NEXT As Integer = 1
        Const SQL_SUCCESS As Integer = 0
        Public Sub FetchDSNs()
            Dim ReturnValue As Short
            Dim DSNName As String
            Dim DriverName As String
            Dim DSNNameLen As Short
            Dim DriverNameLen As Short
            Dim SQLEnv As Integer 'handle to the environment
            If SQLAllocEnv(SQLEnv) <> -1 Then
                Do Until ReturnValue <> SQL_SUCCESS
                    DSNName = Space(1024)
                    DriverName = Space(1024)
                    ReturnValue = SQLDataSources(SQLEnv, SQL_FETCH_NEXT, DSNName, 1024, DSNNameLen, DriverName, 1024, DriverNameLen)
                    DSNName = Left(DSNName, DSNNameLen)
                    DriverName = Left(DriverName, DriverNameLen)
                    If DSNName <> Space(DSNNameLen) Then
                    End If
            End If
        End Sub
    Microsoft MVP (Visual Basic)

Similar Threads

  1. RegCreateKey; RegSetValueEx; RegCloseKey API Problems
    By Patrick Kenney in forum VB Classic
    Replies: 4
    Last Post: 08-21-2001, 06:21 PM
  2. RegCreateKey; RegSetValueEx; RegCloseKey API Problems
    By Patrick Kenney in forum VB Classic
    Replies: 0
    Last Post: 08-21-2001, 05:01 PM
  3. SAX api :problems
    By prateek in forum XML
    Replies: 0
    Last Post: 10-09-2000, 12:09 AM
  4. Scaling problems using API Graphics
    By Allen Johnson in forum VB Classic
    Replies: 2
    Last Post: 04-28-2000, 11:00 AM
  5. Scaling problems using API Graphics
    By Allen Johnson in forum VB Classic
    Replies: 0
    Last Post: 04-26-2000, 12:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center