GetProfileItem.

M

Mr. X.

Hello.
I want to convert the follows to C# :
(Something I saw good for VB).
========================
Private Declare Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" _
(ByVal lpSectionName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long

....
Public Shared Function ProfileGetItem(ByVal lpSectionName As String, _
ByVal lpKeyName As String, _
ByVal defaultValue As String, _
ByVal inifile As String) As String

Dim success As Long
Dim nSize As Long
Dim ret As String
Dim res As String

'call the API with the parameters passed.
'The return value is the length of the string
'in ret, including the terminating null. If a
'default value was passed, and the section or
'key name are not in the file, that value is
'returned. If no default value was passed (""),
'then success will = 0 if not found.

res = ""
'Pad a string large enough to hold the data.
ret = Space$(2048)
nSize = Len(ret)
success = GetPrivateProfileString(lpSectionName, _
lpKeyName, _
defaultValue, _
ret, _
nSize, _
inifile)

If success Then
res = Left$(ret, success)
End If
Return res
End Function

In C# the following doesn't work :
=======================
[DllImport("kernel32", EntryPoint = "GetPrivateProfileStringA", CharSet =
CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int GetPrivateProfileString(string lpSectionName,
string lpKeyName, string lpDefault, string lpReturnedString, long nSize,
string lpFileName);

public static string ProfileGetItem(string lpSectionName, string
lpKeyName, string defaultValue, string inifile)
{

int success = 0;
long nSize = 0;
string ret = null;
string res = null;

//call the API with the parameters passed.
//The return value is the length of the string
//in ret, including the terminating null. If a
//default value was passed, and the section or
//key name are not in the file, that value is
//returned. If no default value was passed (""),
//then success will = 0 if not found.

res = "";
//Pad a string large enough to hold the data.
ret = Strings.Space(2048);
nSize = Strings.Len(ret);
success = GetPrivateProfileString(lpSectionName, lpKeyName,
defaultValue, ret, nSize, inifile);

if (success > 0)
{
res = ret.Substring(0, success);
}
return res;
}

ret is Blank (After GetProfileString).
I suppose I ret should be a pointer to the string, but it conflicts the
calling method.

Thanks :)
 
M

Mr. X.

Thanks :)

Peter Duniho said:
Mr. X. said:
[...]
In C# the following doesn't work :
=======================
[DllImport("kernel32", EntryPoint = "GetPrivateProfileStringA",
CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int GetPrivateProfileString(string
lpSectionName, string lpKeyName, string lpDefault, string
lpReturnedString, long nSize, string lpFileName);

public static string ProfileGetItem(string lpSectionName, string
lpKeyName, string defaultValue, string inifile)
{
[...]
res = "";
//Pad a string large enough to hold the data.
ret = Strings.Space(2048);
nSize = Strings.Len(ret);
success = GetPrivateProfileString(lpSectionName, lpKeyName,
defaultValue, ret, nSize, inifile);

if (success > 0)
{
res = ret.Substring(0, success);
}
return res;
}

ret is Blank (After GetProfileString).
I suppose I ret should be a pointer to the string, but it conflicts the
calling method.

I'm not that familiar with VB.NET-specific interop features. But
generally, in C# you would use a StringBuilder rather than String for
output arguments for interop methods. You might try adjusting your
p/invoke declaration to try it that way.

Pete
 
J

Jeff Johnson

I want to convert the follows to C# :
(Something I saw good for VB).
========================
Private Declare Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" _
(ByVal lpSectionName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long

I really really suggest that you try to abandon virtually everything your
current VB program is doing and just adopt the newer, .NET-style way of
doing things. I understand if such a thing isn't possible due to time or
budget contraints, but if it IS possible then please give up on these
ancient VB conventions (like .INI files*) and move your app (and coding
practices) into the 21st century.



*Yes, I realize .INI files are not specific to VB, but VB programmers seem
to have clung to them much longer than C[++] programmers did.
 
A

Arne Vajhøj

I want to convert the follows to C# :
(Something I saw good for VB).
========================
Private Declare Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" _
(ByVal lpSectionName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long

Lookup the functions somewhere.

SharpDevelop has this feature where you go into
edit
insert
p/invoke signature
and give it the function name it delivers a declaration.

There are also sites on the web that has such declarations.

Like:
http://www.pinvoke.net/

Arne
 
M

Mr. X.

O.K.
What is it ?
xml style.
app.config ?
Any suggested link ?

Thanks :)

Jeff Johnson said:
I want to convert the follows to C# :
(Something I saw good for VB).
========================
Private Declare Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" _
(ByVal lpSectionName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long

I really really suggest that you try to abandon virtually everything your
current VB program is doing and just adopt the newer, .NET-style way of
doing things. I understand if such a thing isn't possible due to time or
budget contraints, but if it IS possible then please give up on these
ancient VB conventions (like .INI files*) and move your app (and coding
practices) into the 21st century.



*Yes, I realize .INI files are not specific to VB, but VB programmers seem
to have clung to them much longer than C[++] programmers did.
 
J

Jeff Johnson

O.K.
What is it ?
xml style.
app.config ?
Any suggested link ?

Using a settings file (i.e., app.config) is a good "starter" method. It's
what I've been doing until recently when I began adopting my co-worker's
style of using custom configuration sections. The only thing I dislike about
them is that data has to be stored in attributes, not text nodes.

If you want full control (and full responsibility!), then XML is the way to
go.

The moment you want to create a collection of items you'll find out how
limiting INI files are. Do you create Item1, Item2, Item3, etc. entries? Do
you create an Items entry and then come up with some sort of delimiting
scheme? At that point you're writing a custom parser anyway, so the API
functions for dealing with INIs suddenly do only part of the job. I just
think INIs had their time and it's passed.
 
A

Arne Vajhøj

Jeff Johnson said:
Mr. X. said:
I want to convert the follows to C# :
(Something I saw good for VB).
========================
Private Declare Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" _
(ByVal lpSectionName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long

I really really suggest that you try to abandon virtually everything
your current VB program is doing and just adopt the newer, .NET-style
way of doing things. I understand if such a thing isn't possible due
to time or budget contraints, but if it IS possible then please give
up on these ancient VB conventions (like .INI files*) and move your
app (and coding practices) into the 21st century.

*Yes, I realize .INI files are not specific to VB, but VB programmers
seem to have clung to them much longer than C[++] programmers did.
O.K.
What is it ?
xml style.
app.config ?
Any suggested link ?

app.config !

Google on:
C# app.config
gives plenty of hits.

Arne
 

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

Similar Threads

How is it work? 4
GetPrivateProfileString 1
use GetPrivateProfileString 2
Reading INI File 1
GetPrivateProfileString 2
GetPrivateProfileString 5
ini file 2 2
Create a DLL with VB.NET and use with VBScript 2

Top