Solved

An executable for Win16 and Win32 Platforms - Using VB3

Posted on 1998-08-23
16
355 Views
Last Modified: 2008-02-01
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
Comment
Question by:mlkf
  • 5
  • 4
  • 4
  • +2
16 Comments
 
LVL 3

Expert Comment

by:a111a111a111
Comment Utility
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
 

Author Comment

by:mlkf
Comment Utility
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
 
LVL 3

Expert Comment

by:a111a111a111
Comment Utility
Woo, Sorry I forget that "YOUR VB 3.0 IS A 32 BIT APPICATION" !!!
0
 
LVL 4

Expert Comment

by:mcix
Comment Utility
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
 

Author Comment

by:mlkf
Comment Utility
mcix, Can you please add more explaination to your suggestion? I did not understand it.

0
 
LVL 4

Expert Comment

by:mcix
Comment Utility
Before I explain...

Are the Win 3.x clients running Win32s?
0
 
LVL 4

Expert Comment

by:mcix
Comment Utility
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
 
LVL 4

Expert Comment

by:mcix
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 14

Expert Comment

by:waty
Comment Utility
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
 
LVL 7

Expert Comment

by:kamall
Comment Utility
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
 

Author Comment

by:mlkf
Comment Utility
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
 

Author Comment

by:mlkf
Comment Utility
Adjusted points to 210
0
 
LVL 7

Accepted Solution

by:
kamall earned 260 total points
Comment Utility
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
 

Author Comment

by:mlkf
Comment Utility
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
 
LVL 7

Expert Comment

by:kamall
Comment Utility
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
 
LVL 7

Expert Comment

by:kamall
Comment Utility
mlkf,
Sorry, I meant 1 sub and 4 functions.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now