Jump to content
Excelsior Forums
Sign in to follow this  
tkleiber

no result from kernel32l GetPrivateProfileS

Recommended Posts

Hi!

I have successfully built an implementation of the kernel32.dll WritePrivateProfileStringA and create my ini file with sections and values.

But when I build a n implemantation GetPrivateProfileStringA the procedure returns without results. In windows SDK this is defined as

DWORD GetPrivateProfileString(

?LPCTSTR lpAppName,

?LPCTSTR lpKeyName,

?LPCTSTR lpDefault,

?LPTSTR lpReturnedString,

?DWORD nSize,

?LPCTSTR lpFileName

);

This is the implemematation:

?private boolean getString(String i_sAppName, String i_sKeyName,

? ?String i_sDefault, String i_sReturnedString, int i_nSize, String i_sFileName) {

? ?try {

? ? ?xFunction oFunction = new xFunction("kernel32",

? ? ? ? ?"int GetPrivateProfileStringA(CSTRING, CSTRING, CSTRING, CSTRING, int, CSTRING)");

? ? ?Argument[] arrArgs = new Argument[6];

? ? ?arrArgs[0] = new Argument(i_sAppName, Argument.CSTRING);

? ? ?arrArgs[1] = new Argument(i_sKeyName, Argument.CSTRING);

? ? ?arrArgs[2] = new Argument(i_sDefault, Argument.CSTRING);

? ? ?arrArgs[3] = new Argument(i_sReturnedString, Argument.CSTRING);

? ? ?arrArgs[4] = new Argument(i_nSize);

? ? ?arrArgs[5] = new Argument(i_sFileName, Argument.CSTRING);

? ? ?int intRet = ((Integer) oFunction.invoke(arrArgs)).intValue();

? ? ?System.out.println("intRet>" + intRet + "<");

? ? ?String sValue0 = (String) arrArgs[0].getValue();

? ? ?System.out.println("sValue0>" + sValue0 + "<");

? ? ?String sValue1 = (String) arrArgs[1].getValue();

? ? ?System.out.println("sValue1>" + sValue1 + "<");

? ? ?String sValue2 = (String) arrArgs[2].getValue();

? ? ?System.out.println("sValue2>" + sValue2 + "<");

? ? ?String sValue3 = (String) arrArgs[3].getValue();

? ? ?System.out.println("sValue3>" + sValue3 + "<");

? ? ?int intValue4 = ((Integer) arrArgs[4].getValue()).intValue();

? ? ?System.out.println("intValue4>" + intValue4 + "<");

? ? ?String sValue5 = (String) arrArgs[5].getValue();

? ? ?System.out.println("sValue5>" + sValue5 + "<");

? ? ?return (true);

? ?} catch (xFunctionException e) {

? ? ?System.out.println(e.getMessage());

? ? ?return (false);

? ?}

?}

If I make a call to an existent ini file, with any combination of existent or nonexistent section and value combination I get:

...

sValue3><

intValue4>0<

...

I don't no what's wrong? Do I have to create the pointer for sValue3? How to this with 6 arguments? Or has it somthing to do with lpReturnedString, which should deliver a StringBuffer in my Opinion?

regards

Torsten Kleiber

Share this post


Link to post
Share on other sites

Hi,

The parameter lpReturnedString should point to a buffer with a sufficient size (nSize). You can construct such buffer with the following

i_nSize = 1024; //the maximum length of the string returned
i_nReturnedString = new Argument("", Argument.CSTRING, i_nSize);

Other arguments seems to be OK. If the problem would persist, it is worth to analyze the returned value (intRet), and probably the system error code set (by calling GetLastError() from kernel32).

Share this post


Link to post
Share on other sites

For this problem this was the solution. So I wrote a error formatting routine for reading system messages:

public class error {

 public String formatMessage(int i_intMessageId) {

   try {

     xFunction oFunction = new xFunction("kernel32",

         "int FormatMessageA(CSTRING, int, int, int, CSTRING, int, CSTRING)");

     System.out.println("i_intMessageId>" + i_intMessageId + "<");

     Argument[] arrArgs = new Argument[7];

     arrArgs[0] = new Argument("FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM ",

         Argument.CSTRING);

     arrArgs[1] = new Argument(0);

     arrArgs[2] = new Argument(i_intMessageId);

     arrArgs[3] = new Argument(0);

     arrArgs[4] = new Argument("", Argument.CSTRING, 65536);

     arrArgs[5] = new Argument(65536);

     arrArgs[6] = new Argument("", Argument.CSTRING);

     int intRet = ((Integer) oFunction.invoke(arrArgs)).intValue();

     String sMessage = (String) arrArgs[4].getValue();

     System.out.println("intRet>" + intRet + "<");

     System.out.println("sMessage>" + sMessage + "<");

     return sMessage;

   } catch (xFunctionException e) {

     System.out.println(e.getMessage());

     return null;

   }

 }

}

But it does nor return the message:

i_intMessageId>87<

intRet>0<

sMessage><

What is wrong here?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×