GetPrivateProfileString en .net

G

Guest

"
You can fix this by declaring sBuf as a normal string and initializing it to
a fixed length with the Space function:
Dim sBuf As String = Space(128)The new VB.NET declaration syntax lets you
declare and initialize the variable on the same line. You use the sBuf
string's Length method to return its length. The function call to return an
INI string now looks like this:

lRet = GetPrivateProfileString (Section, KeyName, _
DefaultValue, sBuf, sBuf.Length, msININame)"Segun Microsoft esta es la
forma de implementar esta API en .net, pero no funciona,¿Alguien sabe por
qué?Saludosjmpedrero
 
T

Tom Shelton

"
You can fix this by declaring sBuf as a normal string and initializing it to
a fixed length with the Space function:
Dim sBuf As String = Space(128)The new VB.NET declaration syntax lets you
declare and initialize the variable on the same line. You use the sBuf
string's Length method to return its length. The function call to return an
INI string now looks like this:

lRet = GetPrivateProfileString (Section, KeyName, _
DefaultValue, sBuf, sBuf.Length, msININame)"Segun Microsoft esta es la
forma de implementar esta API en .net, pero no funciona,¿Alguien sabe por
qué?Saludosjmpedrero

I'm assuming that this is an answer to a question that I don't see?
But, I would like to make a comment... You should never use the String
data type to return values from API calls. Strings in .NET are
immutable - this means that they can not be changed. Because of this
fact, it causes a great deal of overhead - in both memory and speed to
pass a string to an API routine that is expected to be modified by the
called function... And it in fact will not work in C#.

So, my advice :) Pass it as a System.Text.StringBuilder. That would
mean that the declaration of GetPrivateProfileString would look
something like:

Declare Auto Function GetPrivateProfileString Lib "kernel32" ( _
ByVal lpAppName 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


Then you would use it like:

Dim buffer As New System.Text.StringBuilder(256)
Dim retVal As Integer =
GetPrivateProfileString(
"Section", "KeyName", "", buffer, buffer.Capacity, "my.ini")

Dim value As String = buffer.ToString()

HTH
 
H

Herfried K. Wagner [MVP]

* said:
You can fix this by declaring sBuf as a normal string and initializing it to
a fixed length with the Space function:
Dim sBuf As String = Space(128)The new VB.NET declaration syntax lets you
declare and initialize the variable on the same line. You use the sBuf
string's Length method to return its length. The function call to return an
INI string now looks like this:

lRet = GetPrivateProfileString (Section, KeyName, _
DefaultValue, sBuf, sBuf.Length, msININame)"Segun Microsoft esta es la
forma de implementar esta API en .net, pero no funciona,¿Alguien sabe por
qué?Saludosjmpedrero

See:

<http://www.mentalis.org/soft/class.qpx?id=6>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top