Manipulating String in ADK

Hello Amibroker Community,

I recently stuck with an error in simple string concatenation operation in Amibroker development kit.
I may be missing some basic thing but I didn't find anything.

While I was successfully able to do the same with the usual C++ application code but not in ADK.

Why is there is a difference in behavior between a normal string and AmiVar string?
Please guide.

Here is my code:

AmiVar StringFormatTest(int ArgsNum, AmiVar* Args)
{
	AmiVar Result = gSite.AllocArrayResult();

	Result.type = VAR_STRING;

	AmiVar S1, S2;

	S1.type = VAR_STRING;
	S2.type = VAR_STRING;

	S1.string = "Hello";
	S2.string = " World!";

	Result.string = S1.string + S2.string;

	return Result;
}

AmiVar is a simple struct (actually a type and a union). dot string is one of the field, there's no '+' operator defined for it.

You have two options: either work on C++ strings and assign result to AmiVar .string or define a + operator for AmiVar struct. See for instance:
https://en.cppreference.com/w/cpp/language/operators
Regards

In your DLL You need to:

  1. allocate memory for result (sum of lengths of strings + 1)
  2. call C runtime strcpy / strcat functions

Google for more info.

General advice is, if you stuggle with such basic things, then DLLS are not for you: 5 reasons why you should NOT write DLLs

1 Like

Thanks, Dear Tomasz and alligator for your quick response.
With this clarification and a lot of basic exercises, I finally reached to the solution.
After exercise I could now understand that AmiVar string type is not a "C string" but a "char*" and so I cannot treat it like a normal string. While working with afl and float arrays in dll mostly, I missed some basic concepts but now I have refreshed them and working for me. Thank you all.

Here is the code:

AmiVar StringFormatTest(int ArgsNum, AmiVar* Args)
{
	AmiVar S1, S2;

	S1.type = VAR_STRING;
	S2.type = VAR_STRING;

	S1.string = "Hello";
	S2.string = "World!";

	int size = sizeof(char)*(strlen(S1.string) + strlen(S2.string) + 1);
	
	AmiVar Output;
	Output.type = VAR_STRING;
	Output.string = (char*)gSite.Alloc(size);
	
	strcpy_s(Output.string, size, S1.string);
	strcat_s(Output.string, size, S2.string);

	return Output;
}