DllImport Question

grip003

Well-known member
Joined
Sep 2, 2004
Messages
89
Location
North Carolina
This might be a dumb question, but I dont even have DllImport in my System.Runtime.InteropServices. How do I use DllImport if it is not even listed? Im trying to set the affinity of my program before/as the application is started. If there is another way other than using the kernel32.dll with DllImport, then I would love to know how to do this.

Thanks.
 
grip003 said:
This might be a dumb question, but I dont even have DllImport in my System.Runtime.InteropServices. How do I use DllImport if it is not even listed? Im trying to set the affinity of my program before/as the application is started. If there is another way other than using the kernel32.dll with DllImport, then I would love to know how to do this.

Thanks.


At the top you do this

Imports System.Runtime.InteropServices

Then to use it you do something like this. In my example "Haft4K" is the name of the dll.

Code:
   <DllImport("Haft4k", EntryPoint:="SetUserPS", CharSet:=CharSet.Ansi, SetLastError:=True)> _
 Public Shared Function SetUserPS(ByVal nChannel As Integer, ByVal lState As Integer) As Integer
    End Function
 
DllImport should definitely be there. Perhaps it is hidden because it is an advanced member? (Are you using VB?) What happens if you type it in manually and try to compile?
 
Sorry it took me so long to get back to this, I just moved offices and had such a mess. Anyway, I have the DllImport ability now (dont know what was happening) but I dont know how to use it in C#. I need to import kernel32.dll to set the affinity of my application.
 
All set

Well, it turns out that setting the affinity for the application is quite simple. Here is the code:

Code:
System.Diagnostics.Process process = 
   System.Diagnostics.Process.GetCurrentProcess();
process.ProcessorAffinity = new IntPtr(1);

Hope this helps anyone else who ran into the affinity problem.
 
Back
Top