• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

An executable for Win16 and Win32 Platforms - Using VB3

I have an application that I wrote in VB3. This application has to run under win3.x, win95 and NT (Win16 and Win32 platforms).

The problem is:
 I used the WritePrivateProfileString API at this application, declared as one of the kernel.dll , to write to INI file. It works fine. BUT under Win32 the INI files are missing some of the data and are very mess.

Is there a way to do one application for Win16 and Win32 platforms using this API?
(I know that this API declared in Kernel - for Win16 and for win32 in Kernel32)
Does someone has a solution?

((Currently I use:
    Declare Function WritePrivateProfileString Lib "Kernel"     (ByVal lpApplicationName As String, ByVal lpKeyName As      String, ByVal lpString As String, ByVal lplFileName As      String) As Integer
))

Thank You
mlkf
0
mlkf
Asked:
mlkf
  • 5
  • 4
  • 4
  • +2
1 Solution
 
a111a111a111Commented:
Mlkf,
Hi, You can go by this concept:

#If Win32 Then
..
#ElseIf Win16 Then
.

And here is a sample.

#If Win32 Then

Public Function CaptureWindow(ByVal hWndSrc As Long, _

       ByVal Client As Boolean, ByVal LeftSrc As Long, _
       ByVal TopSrc As Long, ByVal WidthSrc As Long, _
       ByVal HeightSrc As Long) As Picture
       Dim hDCMemory As Long
       Dim hBmp As Long
       Dim hBmpPrev As Long
       Dim r As Long
       Dim hDCSrc As Long
       Dim hPal As Long
       Dim hPalPrev As Long
       Dim RasterCapsScrn As Long
       Dim HasPaletteScrn As Long
       Dim PaletteSizeScrn As Long
#ElseIf Win16 Then

Public Function CaptureWindow(ByVal hWndSrc As Integer, _

       ByVal Client As Boolean, ByVal LeftSrc As Integer, _
       ByVal TopSrc As Integer, ByVal WidthSrc As Long, _
       ByVal HeightSrc As Long) As Picture
       Dim hDCMemory As Integer
       Dim hBmp As Integer
       Dim hBmpPrev As Integer
       Dim r As Integer

       Dim hDCSrc As Integer
       Dim hPal As Integer
       Dim hPalPrev As Integer
       Dim RasterCapsScrn As Integer
       Dim HasPaletteScrn As Integer
       Dim PaletteSizeScrn As Integer
#End If


0
 
mlkfAuthor Commented:
Thank You a111a111a111 for your response, But there are two reasons why your answer did not meet my needs:
1. I am using VB3, therefor I can not use #if and #elseif since these are part of VB4 new features.
2. In your suggestion I will have two different executables, and I would like to have only one exe file that will run on both platforms, Win16 and Win32.

Thank You
mlkf
0
 
a111a111a111Commented:
Woo, Sorry I forget that "YOUR VB 3.0 IS A 32 BIT APPICATION" !!!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mcixCommented:
Would you consider calling a VB4 16-bit DLL to determine which OS you are running on and call it to provide the functions you require?
0
 
mlkfAuthor Commented:
mcix, Can you please add more explaination to your suggestion? I did not understand it.

0
 
mcixCommented:
Before I explain...

Are the Win 3.x clients running Win32s?
0
 
mcixCommented:
I don't have VB3 loaded any more si I can't test this, but this may work:

Declare Function GetVersion16 Lib "Kernel" Alias "GetVersion" () As Long
Declare Function WriteProfilePrivateString16 Lib "Kernel" Alias "WritePrivateProfileString" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lplFileName As String) As Integer
Declare Function WritePrivateProfileString32 Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Sub Command1_Click()
If OS32() Then
   mlngResult = WritePrivateProfileString32(lpApplicationName, lpKeyName, lpString, lpFileName)
Else
   mlngResult = WritePrivateProfileString16(lpApplicationName, lpKeyName, lpString, lpFileName)

End If
   
End Sub
     
Private Function OS32() As Boolean

      On Error GoTo err_OS32
      Dim mlngTemp As Long
     
      mlngTemp = GetVersion16()
      OS32 = False


exit_OS32:
    Exit Function
err_OS32:
    Select Case Err.Number
        Case 48
            ' Kernel - File Not Found
            OS32 = True
        Case Else
       
            MsgBox "Some Other error", vbInformation, Err.Description & " - " & Err.Number
           
            Resume exit_OS32
    End Select
End Function
0
 
mcixCommented:
Ooops...

Declare Function WriteProfilePrivateString16 Lib "Kernel" Alias "WritePrivateProfileString" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lplFileName As String) As Integer

Should be:

Private Declare Function WritePrivateProfileString16 Lib "Kernel" Alias "WritePrivateProfileString" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lplFileName As String) As Integer
0
 
watyCommented:
I don't see any problem with my VB3 applitations running either under Win 3.11, Win95, Win98 Win NT. This application uses INI files.

When you run a VB3 application under a 32 bits OS, this 32 bits OS "emulate" a 16 bits OS. There will be a problem if you want to access the Win32 API from your VB3 application.
0
 
kamallCommented:
I will suggest that you write directly to your INI file using the file access statements Write #X, ... and Input #X, ... . They will do the job for you without any problems. You can even write some simple code to do exactly what the WritePrivateProfileString and ReadPrivateProfileString functions will do.
If you need more details, comment me.
0
 
mlkfAuthor Commented:
Kamall,

Your suggestion looks the best.
But I would like to get more details, because I tried to solve this problem by writing my routine. and I have some difficulties with it.

I have to:
Write new INI file.
Update Existing file - change keys value.
Add new keys to an existing sections.
Etc.
0
 
mlkfAuthor Commented:
Adjusted points to 210
0
 
kamallCommented:
mlkf,
Sorry for being away for a few days.
I will make and post you the code within 2 days if you can wait (because really I have no time at all at this moment). If you cann't wait, please reject my answer.
Regards.
0
 
mlkfAuthor Commented:
I Accept your answer. BUT Please do not forget to provide me the sources.
I increased the points for your job.
Thank You
mlkf
0
 
kamallCommented:
mlkf,
Very sorry for the delay. I have been running to hospitals the last few days due to health problems.
I have developed the code for you.
Here are 5 functions that will:
- Create an INI file
- Create a new section
- Create a new key
- Set a key value
- Read the key value

All functions (except the CreatIniFile) will return 0 on success, 1 on failure.
The INI file is FULLY compatible with the standard windows INI file format.

If you have any difficulties, please comment me.
Also, If you need a working project, leave me your e-mail address.
Regards.

Sub CreateIniFile(FileName As String)
   
    'If file does not exist, create it
    If Dir(FileName) = "" Then
        Open FileName For Output As #1
        Close 1
    End If

End Sub

Function CreateSection(FileName As String, SectionName As String) As Integer
   
    Open FileName For Input As #1
    'Check if section already exists
    Do While Not EOF(1)
        Line Input #1, X$
        If X$ = "[" & SectionName & "]" Then
            CreateSection = 1
            Exit Function
        End If
    Loop
    Close 1
    DoEvents
   
    'Create the new section
    Open FileName For Append As #1
    Print #1, ""
    Print #1, "[" & SectionName & "]"
    Close 1
    DoEvents
   
End Function

Function CreateKey(FileName As String, SectionName As String, KeyName As String) As Integer

    Dim TempArray() As String
   
    Open FileName For Input As #1
    'Check if the key already exists
    'Also, read all data and store them in array
    I = 0
    Do While Not EOF(1)
        ReDim Preserve TempArray(I + 1)
        Line Input #1, X$
        If X$ = SectionName Then Flag = True    'Specified section found
        If X$ = "" And Flag = True Then Flag = False    'End of section found
        TempArray(I + 1) = X$
        If Left$(X$, Len(KeyName) + 1) = KeyName & "=" And Flag = True Then     'Key already exists
            CreateKey = 1
            Close 1
            Exit Function
        End If
        I = I + 1
    Loop
    Close 1
    DoEvents
   
    'Create the new key
    Flag = False
    Open FileName For Output As #1
    For I = 1 To UBound(TempArray())
        If TempArray(I) = SectionName Then Flag = True
        Print #1, TempArray(I)
        If Flag = True And TempArray(I) = "" Or I = UBound(TempArray()) Then
            Print #1, KeyName & "="
            Flag = False
        End If
    Next
    Close 1

End Function

Function SetKeyValue(FileName As String, SectionName As String, KeyName As String, KeyValue As String) As Integer

    Dim TempArray() As String
   
    Open FileName For Input As #1
    'First check if the key exists
    'Also, read all data and store them in array
    I = 0
    Do While Not EOF(1)
        ReDim Preserve TempArray(I + 1)
        Line Input #1, X$
        TempArray(I + 1) = X$
        If Left$(X$, Len(KeyName) + 1) = KeyName & "=" Then KeyExist = True
        I = I + 1
    Loop
    Close 1
    DoEvents
    If KeyExist = False Then
        SetKeyValue = 1
        Exit Function
    End If
   
    'Set new key-value
    Open FileName For Output As #1
    For I = 1 To UBound(TempArray())
        If TempArray(I) = "[" & SectionName & "]" Then Flag = True
        If Flag = True And Left$(TempArray(I), Len(KeyName) + 1) = KeyName & "=" Then
            Print #1, KeyName & "=" & KeyValue
            Flag = False
        Else
            Print #1, TempArray(I)
        End If
    Next
    Close 1

End Function

Function GetKeyValue(FileName As String, SectionName As String, KeyName As String, KeyValue As String) As Integer

    Dim TempArray() As String
   
    Open FileName For Input As #1
    Do While Not EOF(1)
        Line Input #1, X$
        If X$ = "[" & SectionName & "]" Then Flag = True
        If Left$(X$, Len(KeyName) + 1) = KeyName & "=" And Flag = True Then
            KeyValue = Right$(X$, Len(X$) - (Len(KeyName) + 1))
            Close 1
            Exit Function
        End If
    Loop
    Close 1
    DoEvents
    GetKeyValue = 1
   
End Function


0
 
kamallCommented:
mlkf,
Sorry, I meant 1 sub and 4 functions.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now