I ended up a simple program sending and receiving a RS-232 message. My parameters (baudrate, COM port) are stored in an INI file (the file will be created automatically if not existing). The progam runs without error but I don't know why it cuts off the path that point to INI file when the path length exceed a limit (there are Unicode Japanese characters in the path string)
My path string in New function (construction) is like this: "D:\通信プログラム20120709\新しいフォルダー\新しいフォルダー\新しいフォルダー\新しいフォルダー\Debug\Config.ini"
and in an event function, it will become: D:\通信プログラム20120709\新しいフォルダー\新しいフォルダー\新しいフォ・
After consulting some source on Internet, they show me that a .NET String would have a very huge capacity so I guess my problem not concerned to VB.NET String.
Any help would be appreciated.
From Comments
I found that the path is changed after the first line of code below
RS232TransPort = IniRoutine.GetString(IniSectionName, ConfigName.COMPort, "COM3")
RS232Baudrate = IniRoutine.GetInteger(IniSectionName, ConfigName.Baudrate, 9600)
This is the function to get a string:
Public Function GetString(ByVal Section As String, ByVal Key As String, ByVal [Default] As String) As String
Dim intCharCount As Integer
Dim objResult As New System.Text.StringBuilder(256)
intCharCount = GetPrivateProfileString(Section, Key, [Default], objResult, objResult.Capacity, strFilename)
GetString = String.Empty
If intCharCount > 0 Then GetString = Left(objResult.ToString, intCharCount)
End Function
where strFilename is a local variable of this class.
And this is the API declaire:
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
You are using GetPrivateProfileStringA instead of GetPrivateProfileStringW.
Since you are using Unicode, you need to use GetPrivateProfileStringW instead.
Related
I'm trying to read a INI file then dump the values into the registry for a setup application to function.
The INI file looks like this
[Setup Components]
Sybase Adaptive Server Anywhere Client=Yes
Borland Database Engine=Yes
BDERoot=c:\Program Files\Borland\BDE
Program Shortcuts=Yes
ODBC Data Service Name=Yes
Local Client = Yes
Sybase Admin = Yes
Sybase Directory= C:\Program Files\SQL Anywhere 16
DBRoot=c:\Database
Word Link=Yes
Installation Root Folder=c:\program
FireDAC=DB=asa16;eng=ENGINE;dbn=DBNAME;links=TCPIP{Host=127.0.0.1;Port=2638}
[Program Shortcuts]
Desktop=Yes
Start Menu=Yes
Program Title=PROGRAM
EXE Pathname=c:\program.exe
Parameters=DBNAME
Starting Directory=c:\
Icon=icon.ICO
[ODBC Data Service Name]
Data Service Name=DBNAME
Database File Name=c:\database\database.db
Database Name=DBNAME
Server Name=ENGINE
Comm Links=TCPIP{}
PrefetchBuffer=6000
PrefetchRows=50
CommBufferSize=1460
Compress=no
[Service]
Service Name=SQLANYs_DBNAME
Display Name=SQL Anywhere - DBNAME
Service Group=SQLANYServer
Params=-n DBNAME -x TCPIP{} "C:\database\database.db" -n DBNAME
So I need all those items to be dumped into the registry easily.
I'm using Visual Studio 2015, I did attempt to use Ludvik Jerabek's INI reader (http://www.codeproject.com/Articles/21896/INI-Reader-Writer-Class-for-C-VB-NET-and-VBScript) but had no luck getting that to function!
The code i did use for the above was the following:
Dim ini = New IniFile()
ini.Load("setup.ini")
Dim readValue = ini.Sections("Service").Keys("Service Name")
MessageBox.Show(readValue.ToString)
When running this code i got the following error : "Conversion from string "Service" to type "Integer" is not valid. -Also this method means naming each and every key in the INI file which would be quite some task!
I then went down another method after reading some help questions on here and i used the following:
Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (ByVal lpAppName As String,
ByVal lpKeyName As String,
ByVal lpDefault As String,
ByVal lpReturnedString As StringBuilder,
ByVal nSize As Integer,
ByVal lpFileName As String) As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sb As StringBuilder
sb = New StringBuilder(500)
Dim readVal = GetPrivateProfileString("Service", "Service Name", "", sb, sb.Capacity, "setup.ini")
MessageBox.Show(readVal.ToString)
End Sub
However this just returns "0"
Any help would be grateful to find a way to get this reading from the INI and dumping to the registry
Using the given IniFile class, this will get you to the config setting values:
Private Sub INIButtonTest_Click(sender As Object, e As EventArgs) Handles INIButtonTest.Click
Try
Dim iniFilePath As String = "H:\Dev\StackOverflow\StackOverflowTest\StackOverflowApp\bin\Debug\test.ini"
Dim myIniFile As New IniFile
myIniFile.Load(iniFilePath)
Dim myValue As String = getIniValue(myIniFile, "Service", "Service Name")
If Not String.IsNullOrEmpty(myValue) Then
MessageBox.Show(String.Format("Found value: [{0}]", myValue))
End If
Catch ex As Exception
MessageBox.Show(String.Concat("Something went wrong:", ex.Message))
End Try
End Sub
Private Function getIniValue(iniFileInstance As IniFile, sectionName As String, sectionKey As String) As String
Dim myValue As String = String.Empty
For Each sect As IniFile.IniSection In iniFileInstance.Sections
If sect.Name = sectionName Then
For Each key As IniFile.IniSection.IniKey In sect.Keys
If key.Name = sectionKey Then
myValue = key.GetValue
Exit For
End If
Next
Exit For
End If
Next
Return myValue
End Function
As an alternate, the code for the original approach is very close to correct, but GetPrivateProfileString doesn't actually exist in Kernel32.dll. You need to add a W to the name, which makes the corrected code:
' With these imports
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.Text
' Note the W on the function name
Private Declare Auto Function GetPrivateProfileStringW Lib "kernel32" (ByVal lpAppName As String,
ByVal lpKeyName As String,
ByVal lpDefault As String,
ByVal lpReturnedString As StringBuilder,
ByVal nSize As Integer,
ByVal lpFileName As String) As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sb As New StringBuilder(500)
Dim result As Integer = GetPrivateProfileStringW("Service", "Service Name", "", sb, sb.Capacity, "setup.ini")
If result > 0 Then
MessageBox.Show(sb.ToString())
Else
Dim ex As New Win32Exception(Marshal.GetLastWin32Error())
MessageBox.Show(ex.Message)
End If
End Sub
If you prefer not to use the W, or you want to call your function something different, you can also use the DllImportAttribute and change the function declaration to:
<DllImport("Kernel32.dll", CharSet:=CharSet.Auto,
SetLastError:=True, EntryPoint:="GetPrivateProfileStringW")>
Private Shared Function GetPrivateProfileString(ByVal lpAppName As String,
ByVal lpKeyName As String,
ByVal lpDefault As String,
ByVal lpReturnedString As StringBuilder,
ByVal nSize As Integer,
ByVal lpFileName As String) As Integer
End Function
This is what I have used since VB 3.0 and it has worked with all versions of windows since 3.1. There have been a few modifications to make it fit into the conventions of the newer dev tools but essentially they are the same routines.
These must be in a public class or in the public area of a form:
Public Declare Function OSGetPrivateProfileString% Lib "kernel32" Alias "GetPrivateProfileStringA"(ByVal AppName As String, ByVal KeyName As String, ByVal keydefault As String, ByVal ReturnString As String, ByVal NumBytes As Integer, ByVal FileName As String)
Public Declare Function OSWritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA"(ByVal AppName As String, ByVal KeyName As String, ByVal keydefault As String, ByVal FileName As String) As Integer
Then, somewhere in your form's code area or if you have a "functions.vb" class (where the above would be declared) place these two:
Public Shared Function GetINIString(ByVal strItem As String, ByVal strDefault As String, ByVal strSection As String, ByVal filename As String) As String
Const BUFFERSIZE As Integer = 16768
Dim strTemp As New String(Chr(0), BUFFERSIZE)
Dim stringSize As Long = 0
stringSize = OSGetPrivateProfileString%(strSection, strItem, strDefault, strTemp, BUFFERSIZE, filename)
Return Strings.Left(strTemp, stringSize)
End Function
AND
Public Shared Sub PutINIString(strItem As String, strValue As String, strSection As String, iniFilename As String)
Dim i As Integer = 0
i = OSWritePrivateProfileString(strSection, strItem, strValue, iniFilename)
End Sub
This continues to work in VB 2010
Trying out some API programming, I encountered a problem with the capGetDriverDescription function of AVICAP32.dll:
Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, _
ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, _
ByVal cbVer As Integer) As Boolean
As far as I know, the lpszName parameter is a buffer for getting the driver description, and I read somewhere that the buffer should be passed as a reference type (ByRef instead of ByVal), and of course it should be a reference because of storing the information I need after returning from the function. But in this function, it's a value type, and it works fine!
even I tried to change it to ByRef but the application didnt run!
What knowledge am I missing? How can this buffer store my information while it's being passed a value type?
This is how the code calls the function to get available webcams:
Private Sub LoadDeviceList()
On Error Resume Next
Dim strName As String = Space(100)
Dim strVer As String = Space(100)
Dim bReturn As Boolean
Dim x As Integer = 0
Do
bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
If bReturn Then
lst1.Items.Add(strName.Trim)
End If
x += 1
Application.DoEvents()
Loop Until bReturn = False
End Sub
I'll start by saying I don't know much about this subject (marshaling data can be a deep subject), but I think Default Marshaling for Strings (MSDN) may help you. Scroll down to the part that says Fixed-Length String Buffers. According to this, when a string is marshaled to the API, even if passed byRef, it can't be modified by the callee.
I haven't tested, but according to the MSDN example, to get a value back from your function, the definition would become
Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, _
ByVal lpszName As StringBuilder, ByVal cbName As Integer, ByVal lpszVer As String, _
ByVal cbVer As Integer) As Boolean
Changing lpszName from String to StringBuilder. Apparently StringBuilder will work as a buffer to get the string back from the function.
Then you would call your function like so,
StringBuilder sb = new StringBuilder(256)
capGetDriverDescription(Driver, sb, sb.Capacity + 1, ....
return sb.ToString()
I found a very similar question, How do I import and call unmanaged C dll with ANSI C string "char *" pointer string from VB.NET?.
Evening Everyone -
I'm looking for some thoughts on how to read / write values from a windows "ini" structured file. I have a settings file created with another application and I would like to update values of a key within a specified section. I got it working using a buffer.replace process but now realize that some keys are used over in sections and globally replacing a value will cause problems.
Here is a sample of what my ini file looks like
IMPORT-1]
SETTINGS="HELLO"
FILENAME="C:\TEST\TEST1.CSV"
[ENCODE-2]
FILENAME="C:\TEST\REPORT1.XPS"
I've got dozens of blocks so any clarity on accomplishing a read and write of a value within a specific section would be hugely appreciated!
--Cheers & Thanks
George
You can use some of the kernel32 functions.
Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer
This will let you read an ini file
Dim sb As StringBuilder
sb = New StringBuilder(500)
GetPrivateProfileString("IMPORT-1", "SETTINGS", "", sb, sb.Capacity, "test.ini")
I have a path in short version or in DOS format ("C:/DOCUME~1" e.g) and want to get the full path/long path of it ("C:/Documents And Settings" e.g).
I tried GetLongPathName api. It WORKED. But when deal with unicode filename it turns out failure.
Private Declare Function GetLongPathName Lib "kernel32" Alias _
"GetLongPathNameA" (ByVal lpszShortPath As String, _
ByVal lpszLongPath As String, ByVal cchBuffer As Long) As Long
I tried to alias GetLongPathNameW instead but it seems do nothing, for BOTH Unicode and non-Unicode filename, always return 0. In MSDN there's only article about GetLongPathNameW for C/C++, not any for VB/VBA. May I do something wrong?
Is there any solution for this case? I spend hours on Google and StackOverflow but can't find out.
Regards,
Does this work for you? I've converted the file path to short path name then converted it back again which gives the correct string even when unicode (eg C:/Tö+)
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" _
(ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
Private Declare Function GetLongPathName Lib "kernel32" Alias "GetLongPathNameA" _
(ByVal lpszShortPath As String, ByVal lpszLongPath As String, ByVal cchBuffer As Long) As Long
Public Function GetShortPath(ByVal strFileName As String) As String
'KPD-Team 1999
'URL: [url]http://www.allapi.net/[/url]
'E-Mail: [email]KPDTeam#Allapi.net[/email]
Dim lngRes As Long, strPath As String
'Create a buffer
strPath = String$(165, 0)
'retrieve the short pathname
lngRes = GetShortPathName(strFileName, strPath, 164)
'remove all unnecessary chr$(0)'s
GetShortPath = Left$(strPath, lngRes)
End Function
Public Function GetLongPath(ByVal strFileName As String) As String
Dim lngRes As Long, strPath As String
'Create a buffer
strPath = String$(165, 0)
'retrieve the long pathname
lngRes = GetLongPathName(strFileName, strPath, 164)
'remove all unnecessary chr$(0)'s
GetLongPath = Left$(strPath, lngRes)
End Function
Private Sub Test()
shortpath = GetShortPath("C:/Documents And Settings")
Longpath = GetLongPath(shortpath)
End Sub
To use W-functions from vb6/vba, you declare all string parameters as long:
Private Declare Function GetLongPathName Lib "kernel32" Alias "GetLongPathNameW" _
(ByVal lpszShortPath As Long, _
ByVal lpszLongPath As Long, _
ByVal cchBuffer As Long) As Long
and pass StrPtr(a_string) instead of just a_string.
So if you had:
dim s_path as string
dim l_path as string
s_path = "C:\DOCUME~1"
l_path = string$(1024, vbnullchar)
GetLongPathNameA s_path, l_path, len(l_path)
it would become
dim s_path as string
dim l_path as string
s_path = "C:\DOCUME~1"
l_path = string$(1024, vbnullchar)
GetLongPathNameW strptr(s_path), strptr(l_path), len(l_path)
Is it possible to save images using Visual Basic 2008 from URL to my PC?
For example : From www.domain.com/image.jpg to C:\folder\image.jpg
P.S: I need simpliest example of the code, then I will edit is as I need.
Thanks.
Update : I want to know when the code have finished downloading of the image.
This is the simplest way I know.
Dim Client as new WebClient
Client.DownloadFile(Source, Destination)
Client.Dispose
This is superior to using the My.Computer.Network.DownloadFile method per Microsoft's documentation
"The DownloadFile method does not send optional HTTP headers. Some servers may return 500 (Internal Server Error) if the optional user agent header is missing. To send optional headers, you must construct a request using the WebClient class."
There's a simpler way:
My.Computer.Network.DownloadFile(Source, Desination)
Here what i came up with.
Public Function getImgFrmUrl(ByVal url As String, ByVal Optional ImageName As String = "", ByVal Optional DstntnPath As String = "c:\") As String
Dim imgPath = DstntnPath & "\"
Dim name = IIf(ImageName.Length = 0, Guid.NewGuid.ToString, ImageName)
Dim fileExt = Path.GetExtension(url)
Using webClient As WebClient = New WebClient
Const _Tls12 As SslProtocols = CType(&HC00, SslProtocols)
Const Tls12 As SecurityProtocolType = CType(_Tls12, SecurityProtocolType)
ServicePointManager.SecurityProtocol = Tls12
Dim data As Byte() = webClient.DownloadData(url)
If File.Exists(imgPath + name & fileExt) Then File.Delete(imgPath + name & fileExt)
Using mem = New MemoryStream(data)
Using yourImage = Image.FromStream(mem)
If fileExt.ToLower Is ".png" Then
yourImage.Save(imgPath + name & fileExt, ImageFormat.Png)
Else
yourImage.Save(imgPath + name & fileExt, ImageFormat.Jpeg)
End If
End Using
End Using
End Using
Return imgPath & name & fileExt
End Function
create a module and use this function
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Public Function DownloadFile(sURLFile As String, sLocalFilename As String) As Boolean
Dim lRetVal As Long
lRetVal = URLDownloadToFile(0, sURLFile, sLocalFilename, 0, 0)
If lRetVal = 0 Then DownloadFile = True
End Function