none
Can someone help me take a look, did i use a good way to write it how can i improve it RRS feed

  • Question

  •   Try

                boolPrinterActivate = CBool(CInt(RetrieveFromIniFile("COMMPORT_PRINTER", "ENABLE", "0", strIniPath)))

                strSetting = CStr(RetrieveFromIniFile("COMMPORT_PRINTER", "SETTING", "115200,N,8,1", strIniPath))
                arrSetting = Split(strSetting, ",")
                ''strBoudRate = arrSetting(0)
                ''strParity = arrSetting(1)
                ''strDataBit = arrSetting(2)
                ''strStopBit = arrSetting(3)

                SerialPort.Parity = Parity.None
                SerialPort.StopBits = StopBits.One
                SerialPort.Handshake = Handshake.None
                SerialPort.Open()
                SerialPort.ReadTimeout = 200

                strBoudRate = CStr("115200")
                strParity = CStr("N")
                strDataBit = CStr("8")
                strStopBit = CStr("1")
    • Moved by Alberto PoblacionMVP Monday, November 25, 2019 10:38 AM Originally posted in the C# forum, but the code is VB.NET
    Monday, November 25, 2019 6:41 AM

All replies

  •  Public Function RetrieveFromIniFile(ByVal strHeader As String, ByVal strtitle As String, ByVal strDafaultData As String, ByVal strPath As String)
            Dim n As Int32
            Dim sData As String = Space$(1024)
            n = GetPrivateProfileString(strHeader, strtitle, strDafaultData, sData, sData.Length, strPath)
            If n > 0 Then
                Return sData.Substring(0, n)
            Else
                Return strDafaultData
            End If
        End Function
    Monday, November 25, 2019 6:29 AM
  • Well, you posted the question originally in the C# forum. If you were actually trying to compile that code in C# then it's no wonder you got an error, since it's VB, not C#. I have moved your post to the Visual Basic forum.

    One first problem is the use of Space$, which is a function from the original VB6 and earlier. In VB.NET you can replace it with one of the overloads of the String constructor.

    But the main problem is the call to GetPrivateProfileString. This function does not exist in the Framework Class Libraries. If you are trying to call the API, you need to declare the API using <DllImport> or Declare Function. Otherwise it will not be recognized.

    http://www.pinvoke.net/search.aspx?search=GetPrivateProfileString&namespace=[All]

    Monday, November 25, 2019 6:49 AM
  • Hi christing, 

    Thank you for posting here.

    I try to make a test based on your code, but I get many errors, so I need more information to reproduce your problem.

    What's the type of your application? 

    Could you provide the complete and correct code here? It will help us to analyze your problem.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 25, 2019 9:57 AM
    Moderator
  • I try to make a test based on your code, but I get many errors

    No wonder you get many errors. It is VB.NET code, not C#.

    I'll move the message to the VB.NET forum.

    Monday, November 25, 2019 10:37 AM
  • @XINGYU

    What's the type of your application? 

    VISUAL STUDIO 2010 

    the purpose on this program is i want development 1 program can link to hardware and print it out.

    i success fins out the answer this my my code

      Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" _
     Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, _
     ByVal lpKeyName As String, ByVal lpDefault As String, _
     ByVal lpReturnedString As String, ByVal nSize As Int32, _
     ByVal lpFileName As String) As Int32

        Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" _
        Alias "WritePrivateProfileStringW" (ByVal lpApplicationName As String, _
        ByVal lpKeyName As String, ByVal lpString As String, _
        ByVal lpFileName As String) As Int32

    Public Sub OpenMsCommPrinter()

            Dim strSetting As String = ""
            Dim arrSetting As Array
            Dim strBoudRate As String = ""
            Dim strParity As String = ""
            Dim strDataBit As String = ""
            Dim strStopBit As String = ""
            Dim strCommPorName As String = ""

            Try
                boolPrinterActivate = CInt(RetrieveFromIniFile("COMMPORT_PRINTER", "ENABLE", "0", strIniPath))

                strSetting = RetrieveFromIniFile("COMMPORT_PRINTER", "SETTING", "115200,N,8,1", strIniPath)
                arrSetting = Split(strSetting, ",")
                strBoudRate = arrSetting(0)
                strParity = arrSetting(1)
                strDataBit = arrSetting(2)
                strStopBit = arrSetting(3)

                strCommPorName = RetrieveFromIniFile("COMMPORT_PRINTER", "PORTNO", "1", strIniPath)

                If boolPrinterActivate Then

                    Me.spPrinter.BaudRate = strBoudRate
                    If UCase(strParity) = "E" Then
                        Me.spPrinter.Parity = Parity.Even
                    ElseIf UCase(strParity) = "N" Then
                        Me.spPrinter.Parity = Parity.None
                    ElseIf UCase(strParity) = "O" Then
                        Me.spPrinter.Parity = Parity.Odd
                    ElseIf UCase(strParity) = "M" Then
                        Me.spPrinter.Parity = Parity.Mark
                    Else
                        Me.spPrinter.Parity = Parity.Space
                    End If
                End If

                Me.spPrinter.DataBits = strDataBit
                If strStopBit = "1" Then
                    Me.spPrinter.StopBits = StopBits.One
                ElseIf strStopBit = "1.5" Then
                    Me.spPrinter.StopBits = StopBits.OnePointFive
                ElseIf strStopBit = "2" Then
                    Me.spPrinter.StopBits = StopBits.Two
                Else
                    Me.spPrinter.StopBits = StopBits.None
                End If

                If Me.spPrinter.IsOpen = True Then
                    Me.spPrinter.Close()
                End If

            Catch ex As Exception
            End Try

        End Sub

    Private Sub InitializePrinter()

            strPrinterModel = CStr(RetrieveFromIniFile("SETTING", "PRINTER_MODEL", "", strIniPath))
            strPrinterModel = UCase(strPrinterModel)


            If strPrinterModel = "SATO" Then

                str2DSourcePath = strCurrPath + "\2DCode\2DLabelBT_Sato.txt"
            Else
                str2DSourcePath = strCurrPath + "\2DCode\2DLabelBT.txt"
            End If

     Public Function RetrieveFromIniFile(ByVal strHeader As String, ByVal strtitle As String, ByVal strDafaultData As String, ByVal strPath As String)
            Dim n As Int32
            Dim sData As String = Space$(1024)
            n = GetPrivateProfileString(strHeader, strtitle, strDafaultData, sData, sData.Length, strPath)
            If n > 0 Then
                Return sData.Substring(0, n)
            Else
                Return strDafaultData
            End If
        End Function





    • Edited by christing Tuesday, November 26, 2019 11:01 AM
    Tuesday, November 26, 2019 10:54 AM
  • chris,

    Where did your example come from? Looks more like vb6 code or other instead of vb.net?

    For vb.net, unless you have some special needs, you should use the print document features and dialogs built into vb.net.

    https://docs.microsoft.com/en-us/dotnet/api/system.drawing.printing.printdocument?view=netframework-4.8

    Tuesday, November 26, 2019 11:34 AM
  • Used in .Net, your code contains some flaws which were in use by old VB developers, who inherited Cobol style in there code.

    1. For example you declare every dimension first and then use it. You can declare it at the moment you use it the first time. (with the exception that you cannot use it at a higher level if it dimensioned on a lower level part of the code). If you do it like this, your code becomes more readable.
    2. While you use correct CamelCase, you use old prefixes likewise str, arr, don't use it anymore it is so 1980.
    3. You avoid the Select case, in VB a very strong keyword to use. To many Else If's almost anytime leads to confusion and to wrong run time code. Be aware, without knowing it some C# MVP tell not to use it because it is rotten in C#. They simply think it is the same. https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/select-case-statement
    4. Try to use the common use of names in .Net. A Global dimensioned name start with an uppercase, a name dimensioned in a method (Sub, Function) with a lowercase.
    5. Don't use the C# + to concatinate strings, especially when there are numbers used it can go wrong. VB has a very good concatinater the "&"
    6. Put in top of your code Option Strict On, Option Infer On and Option Explicit On. (Be aware never to set Option Strict Off and Option Infer On, that can give strange results).
    7. With Option Infer On, you can avoid all kind of strange things you're doing with the fixed array.   


    Success
    Cor

    Tuesday, November 26, 2019 12:09 PM
  • chris,

    Where did your example come from? Looks more like vb6 code or other instead of vb.net?

    For vb.net, unless you have some special needs, you should use the print document features and dialogs built into vb.net.

    https://docs.microsoft.com/en-us/dotnet/api/system.drawing.printing.printdocument?view=netframework-4.8

    Tommy, 

    It looks like VB6, but in my perception it is easy handed conversion to VB.Net. I'm not very well in W32 api's. However, there is used Int32 instead of Long (which would give an error because of the integer shift between VB6 and VB7)

    Just an eye opener. :-) 

    Cor


    • Edited by Cor Ligthert Tuesday, November 26, 2019 12:19 PM
    Tuesday, November 26, 2019 12:18 PM