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![Smile :) :)](/styles/default/custom/smilies/smile.gif)
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
![Smile :) :)](/styles/default/custom/smilies/smile.gif)