rosi97 rosi97 - 7 months ago 133
Ini Question

VBA GetPrivateProfileString

I want to read a ini-File with

GetPrivateProfileString
.

The problem is that my string contains some characters which shouldnt be there.

Picture

I think that this is coming from
path = String(255,0)
but if I dont write this, my excel just stops working.

Is there a way to bypass this problem or is something wrong with my code?

Sub

Dim path as String,m2_path as string
path = String(255, 0)
m2_path = String(255, 0)

nc = GetPrivateProfileString("PATH", "path1", "", path, 255, inipath)
nc = GetPrivateProfileString("PATH", "path2", "", m2_path, 255, inipath)


Function

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


ini-File

[PATH]
path1="G:\Arbeit\gen molding"
...

cup cup
Answer Source

The value nc tells you how many characters there are in the string. The problem is the actual interface is a character array: not a string. So what you have to do is give it your best guess as to how many characters you can possibly get (nsize) and it will tell you how many characters there are.

dim path1 as string, path2 as string, path as string

path = string(255, 0)
nc = GetPrivateProfileString("PATH", "path1", "", path, 255, inipath)
path1 = left(path, nc)
nc = GetPrivateProfileString("PATH", "path2", "", m2_path, 255, inipath)
path2 = left(path, nc)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download