microsoft / CsWin32

A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.
MIT License
2k stars 84 forks source link

Cannot discover the namespace APIs are emitted into #1023

Closed Aaswin1996 closed 11 months ago

Aaswin1996 commented 11 months ago

Actual behavior

Trying to use CSWin32 to do some Pinvoke calls but my imports are throwing an error

using System;
using System.Linq;
using Microsoft.Windows.SDK <- this cannot be imported says windows does not exist in namespace microsoft ;
using static ScoutLib.Utils.GlobalUtils;
using AddInUtilities;
using System.Diagnostics;
using System.Collections.Generic;
using System.Threading;

namespace ScoutLib.JobObjects
{
    public partial class ScoutModule
    {
        public static void JobObjectHandlerFunc(JobObjectsConfig jobObjectsConfig)
        {
            Dictionary<string, int> processIdsMapping = new();

            // Get the process Ids for the required processes
            foreach (var processName in jobObjectsConfig.BinariesWithCpuUsageCapped)
            {
                var processes = Process.GetProcessesByName(processName);
                if (processes.Length == 0)
                {
                    Log.Debug($"Could not find Process with Name : {processName}");
                }
                else
                {
                    // Get the first running process by name
                    Log.Debug($" Found Process : {processName} with Id {processes[0].Id}");
                    processIdsMapping[processName] = processes[0].Id;
                }
            }

            // Try creating Job Object
            // Create a job object
            var jobObject = PInvoke.S_OK;

        }

        public static void SpawnJobObjectThread()
        {
            Log.Info("Trying to spawn a new thread for Job Objects");
            var jobObjectsConfigManager = new ConfigManager<JobObjectsConfig>("JOB_OBJECTS_CONFIG");
            var jobObjectsConfig = jobObjectsConfigManager.ConfigValues;

            var jobObjectThread = new Thread(() => JobObjectHandlerFunc(jobObjectsConfig));
            jobObjectThread.Start();

            if (!jobObjectThread.IsAlive)
            {
                Log.Warn("Job Object thread could not be spawned .");
                // ToDo : What to do incase thread could not be spawned.
            }
            Log.Info("Successfully spawned a new thread for job objects related handling");
        }
    }
}

Expected behavior

Should be able to call Pinvoke

Repro steps

  1. NativeMethods.txt content:

    S_OK;
  2. NativeMethods.json content (if present):

  3. Any of your own code that should be shared?

Context

AArnott commented 11 months ago

tip: use 3 backticks with a cs after them, on their own line, to delineate c# code blocks for better formatting in github.

AArnott commented 11 months ago

using Microsoft.Windows.SDK <- this cannot be imported says windows does not exist in namespace microsoft ;

If that's the problem you're calling out, you're using the wrong namespace. The namespace we generate into is Windows.Win32. When in doubt, try using Go To All (Ctrl-T) on the identifier you entered into NativeMethods.txt and C# will jump you to the code generated API so you can inspect its namespace.