Hi,
I have a plugin written in C#.
I want to set a stringvar's value, but i dont know how to do it.
Please help!
Here is my example method:
Code
- [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
- public static void AccessStringVariable(ushort variableIndex, [C99Type("char*")] IntPtr firstCharacterAddress, [C99Type("__crt_bool*")] IntPtr writeValue)
- {
- string val = Marshal.PtrToStringUni(firstCharacterAddress);
- if (variableIndex == 10)
- {
- if (val != ConnectionString)
- {
- Log($"In-game ConnectionString: '{val}'");
- Log($"Plugin ConnectionString: '{ConnectionString}'");
- Log($"Setting ConnectionString...");
- try
- {
- //Methods i tried (but not worked)
- //Method 1
- for (int i = 0; i < ConnectionString.Length; i++)
- {
- Marshal.WriteInt16(firstCharacterAddress, i * 2, ConnectionString[i]);
- }
- Marshal.WriteInt16(firstCharacterAddress, ConnectionString.Length * 2, 0);
- Marshal.StructureToPtr<bool>(true, writeValue, false);
- //Method 2
- var chars = Encoding.Unicode.GetBytes($"{ConnectionString}\0");
- Marshal.Copy(chars, 0, firstCharacterAddress, chars.Length);
- Marshal.StructureToPtr<bool>(true, writeValue, false);
- Log($"ConnectionString set!");
- }
- catch (Exception e)
- {
- Log($"Failed to set ConnectionString! Msg: {e.Message}");
- }
- }
- }
- }