✅ problems with executing code from variable

earlier I asked a question in this server how to execute code from variable. Now i have another problem: usual code is executing good, but my code doesn't want to, it execute with error
No description
No description
17 Replies
das Vainstaintisch
das VainstaintischOP7mo ago
code:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using System.Runtime.InteropServices;

namespace Testapp
{
internal class Program
{
static async Task Main(string[] args)
{

var codeToCompile = @"
[DllImport(""user32.dll"")]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
[DllImport(""user32.dll"")]
static extern void keybd_event(byte bGAK, byte bScan, int dwFlags, int dwExtraInfo);
[DllImport(""user32.dll"")]
static extern short GetAsyncKeyState(int GAKey);
if((GetAsyncKeyState(0x26) & 0x8000) != 0)//проверка на стрелочку вверх
{
mouse_event(0x0001, 0, -10, 0, 0);// курсор вверх на 10 пикселей
}

if ((GetAsyncKeyState(0x28) & 0x8000) != 0)//проверка на стрелочку вниз
{
mouse_event(0x0001, 0, 10, 0, 0);// курсор вниз на 10 пикселей
}

if ((GetAsyncKeyState(0x25) & 0x8000) != 0)//проверка на стрелочку налево
{
mouse_event(0x0001, -10, 0, 0, 0);// курсор влево на 10 пикселей
}

if ((GetAsyncKeyState(0x27) & 0x8000) != 0)//проверка на стрелочку направо
{
mouse_event(0x0001, 10, 0, 0, 0);// курсор вправо на 10 пикселей
}
";

var options = ScriptOptions.Default;
options = options.WithImports("System", "System.Console");
Script compiledScript = CSharpScript.Create(codeToCompile, options);

Console.WriteLine("Executing code...");

while (true) { var result = await compiledScript.RunAsync(); }// зацикливает код, всё что дальше - бессмысленно
//Console.WriteLine(result.ReturnValue);
Console.WriteLine("Code executed...");
Console.ReadKey();
}
}
}
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using System.Runtime.InteropServices;

namespace Testapp
{
internal class Program
{
static async Task Main(string[] args)
{

var codeToCompile = @"
[DllImport(""user32.dll"")]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
[DllImport(""user32.dll"")]
static extern void keybd_event(byte bGAK, byte bScan, int dwFlags, int dwExtraInfo);
[DllImport(""user32.dll"")]
static extern short GetAsyncKeyState(int GAKey);
if((GetAsyncKeyState(0x26) & 0x8000) != 0)//проверка на стрелочку вверх
{
mouse_event(0x0001, 0, -10, 0, 0);// курсор вверх на 10 пикселей
}

if ((GetAsyncKeyState(0x28) & 0x8000) != 0)//проверка на стрелочку вниз
{
mouse_event(0x0001, 0, 10, 0, 0);// курсор вниз на 10 пикселей
}

if ((GetAsyncKeyState(0x25) & 0x8000) != 0)//проверка на стрелочку налево
{
mouse_event(0x0001, -10, 0, 0, 0);// курсор влево на 10 пикселей
}

if ((GetAsyncKeyState(0x27) & 0x8000) != 0)//проверка на стрелочку направо
{
mouse_event(0x0001, 10, 0, 0, 0);// курсор вправо на 10 пикселей
}
";

var options = ScriptOptions.Default;
options = options.WithImports("System", "System.Console");
Script compiledScript = CSharpScript.Create(codeToCompile, options);

Console.WriteLine("Executing code...");

while (true) { var result = await compiledScript.RunAsync(); }// зацикливает код, всё что дальше - бессмысленно
//Console.WriteLine(result.ReturnValue);
Console.WriteLine("Code executed...");
Console.ReadKey();
}
}
}
Jimmacle
Jimmacle7mo ago
it's the same error as if you forgot a using in normal C# code
das Vainstaintisch
das VainstaintischOP7mo ago
you mean i should add using DllImportAttribute?'
Jimmacle
Jimmacle7mo ago
i don't use this scripting stuff but you have a line with what look like usings
das Vainstaintisch
das VainstaintischOP7mo ago
i'll try i added this, and nothing was changed still this error
Jimmacle
Jimmacle7mo ago
options = options.WithImports("System", "System.Console"); here?
das Vainstaintisch
das VainstaintischOP7mo ago
var result = await compiledScript.RunAsync(); here's error or what do you mean
Jimmacle
Jimmacle7mo ago
i mean those look like usings, so if it was me i would try adding the namespace to that list on the line i shared
das Vainstaintisch
das VainstaintischOP7mo ago
No description
Jimmacle
Jimmacle7mo ago
that's not a namespace that's just the type name
Jimmacle
Jimmacle7mo ago
DllImportAttribute Class (System.Runtime.InteropServices)
Indicates that the attributed method is exposed by an unmanaged dynamic-link library (DLL) as a static entry point.
Jimmacle
Jimmacle7mo ago
System.Runtime.InteropServices is the namespace just like you would need using System.Runtime.InteropServices; if this was a normal C# program
das Vainstaintisch
das VainstaintischOP7mo ago
but i added this into code in the top.. or in variable with code, you mean, right? sorry if i'm being stupid, but i may not understand you i don't like this site in total because it always gives me not those im looking for
Buddy
Buddy7mo ago
What are you trying to make?
SleepWellPupper
SleepWellPupper7mo ago
I believe he meant for you to add System.Runtime.InteropServices to WithImports, and remove DllImportAttribute from it.
das Vainstaintisch
das VainstaintischOP7mo ago
oh, now it works, thank you! but another one thing can i all these dll imports
[DllImport(""user32.dll"")]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
[DllImport(""user32.dll"")]
static extern void keybd_event(byte bGAK, byte bScan, int dwFlags, int dwExtraInfo);
[DllImport(""user32.dll"")]
static extern short GetAsyncKeyState(int GAKey);
[DllImport(""user32.dll"")]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
[DllImport(""user32.dll"")]
static extern void keybd_event(byte bGAK, byte bScan, int dwFlags, int dwExtraInfo);
[DllImport(""user32.dll"")]
static extern short GetAsyncKeyState(int GAKey);
put into this string?(WithImports) to make code more clean
Jimmacle
Jimmacle7mo ago
no, p/invoke has to be done with attributes
Want results from more Discord servers?
Add your server