PAWN strings to C++ char
#1

So, I'm trying to make a plugin that shows a MessageBox in Windows when a function in pawn is called, but when it shows up the text is made of japanese symbols or something.

How do I retrieve the string values and store them correctly in a C++ char variable?
Reply
#2

You can write plugins in C#. Look up amx_GetString in the implementers guide to convert a PAWN string to a C string.
Reply
#3

Oh, that's so stupid, sorry. I meant C++ haha. Should be the same thing though, thank you a lot! I had never seen that guide before.
Reply
#4

Quote:
Originally Posted by SWEMike
Посмотреть сообщение
Oh, that's so stupid, sorry. I meant C++ haha. Should be the same thing though, thank you a lot! I had never seen that guide before.
No problem ... there's a lot of stuff inside that you have to skip through, but you'll find a complete function reference (and lots of info on the SDK).
Reply
#5

Код:
static cell AMX_NATIVE_CALL MsgBox(AMX *amx, cell *params)
{
	cell *cstr;
	char *text, *caption;
	amx_GetAddr(amx, params[1], &cstr);
	amx_GetString(text, cstr, 0, UNLIMITED);
	amx_GetAddr(amx, params[2], &cstr);
	amx_GetString(caption, cstr, 0, UNLIMITED);
	MessageBoxW(NULL, LPCWSTR(text), LPCWSTR(caption), MB_OK);
	return 1;
}
That's what I've got at the moment.

When I use the command in the server it gives me this window:



EDIT: Oh, changed, so the variables are initialized, now instead I get Japanese text. :S
Reply
#6

pawn Код:
char* text = ""
?
Reply
#7

Edited above post. Now it's initialized, but I get the japanese stuff again.
Reply
#8

Quote:
Originally Posted by SWEMike
Посмотреть сообщение
Код:
static cell AMX_NATIVE_CALL MsgBox(AMX *amx, cell *params)
{
	cell *cstr;
	char *text, *caption;
	amx_GetAddr(amx, params[1], &cstr);
	amx_GetString(text, cstr, 0, UNLIMITED);
	amx_GetAddr(amx, params[2], &cstr);
	amx_GetString(caption, cstr, 0, UNLIMITED);
	MessageBoxW(NULL, LPCWSTR(text), LPCWSTR(caption), MB_OK);
	return 1;
}
That's what I've got at the moment.

When I use the command in the server it gives me this window:



EDIT: Oh, changed, so the variables are initialized, now instead I get Japanese text. :S
You have to allocate memory if you're going to use a char pointer! You could just declare a simple char array instead, if you dont know anything about memory allocation.

Edit: Also, NEVER use "UNLIMITED" for a size parameter again - thats a good way to cause a buffer overflow.
Reply
#9

Here you can give this a try (Its a little sloppy and rushed):

Код:
cell AMX_NATIVE_CALL MsgBox(AMX *amx, cell *params)
{
	cell *addr;
	char *text, *caption;
	int size[2];

	amx_GetAddr(amx, params[1], &addr);
	amx_StrLen(addr, &size[0]);

	if(size[0])
	{
		size[0]++;
		text = new char[ size[0] ];
		amx_GetString(text, addr, 0, size[0]);

		amx_GetAddr(amx, params[2], &addr);
		amx_StrLen(addr, &size[1]);

		if(size[1])
		{
			size[1]++;
			caption = new char[ size[1] ];
			amx_GetString(caption, addr, 0, size[1]);

			MessageBoxW(NULL, LPCWSTR(text), LPCWSTR(caption), MB_OK);

			delete[] text;
			delete[] caption;
			
			return 1;
		}
		delete[] text;
	}
	return 0;
}
If you wanted to you could add a check to make sure the correct value of parameters were passed, but not everyone likes doing this as native declarations pretty much enforce this for you.
Reply
#10

Quote:
Originally Posted by Kyosaur
Посмотреть сообщение
You have to allocate memory if you're going to use a char pointer! You could just declare a simple char array instead, if you dont know anything about memory allocation.

Edit: Also, NEVER use "UNLIMITED" for a size parameter again - thats a good way to cause a buffer overflow.
Oh, I just copied from the implementer guide. Haha, I don't think I know anything about memory allocation. I just program for fun, mostly.

Quote:
Originally Posted by Kyosaur
Посмотреть сообщение
Here you can give this a try (Its a little sloppy and rushed):

Код:
cell AMX_NATIVE_CALL MsgBox(AMX *amx, cell *params)
{
	cell *addr;
	char *text, *caption;
	int size[2];

	amx_GetAddr(amx, params[1], &addr);
	amx_StrLen(addr, &size[0]);

	if(size[0])
	{
		size[0]++;
		text = new char[ size[0] ];
		amx_GetString(text, addr, 0, size[0]);

		amx_GetAddr(amx, params[2], &addr);
		amx_StrLen(addr, &size[1]);

		if(size[1])
		{
			size[1]++;
			caption = new char[ size[1] ];
			amx_GetString(caption, addr, 0, size[1]);

			MessageBoxW(NULL, LPCWSTR(text), LPCWSTR(caption), MB_OK);

			delete[] text;
			delete[] caption;
			
			return 1;
		}
		delete[] text;
	}
	return 0;
}
If you wanted to you could add a check to make sure the correct value of parameters were passed, but not everyone likes doing this as native declarations pretty much enforce this for you.
I tried this and it's still japanese.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)