CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: API with C#?

  1. #1
    Join Date
    Apr 2003
    Posts
    18

    API with C#?

    I need to work with the windows api function CreateThread(...) in c#, but i can't. I may not use the Thread class (it's for a school project and the teacher wants that). Can anybody help me? Regards, Cris

  2. #2
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    I have a good feeling today, so I am not going to abuse you with things like: it's a school projet so read the documentation and learn...

    Code:
    using System;
    using System.Runtime.InteropServices;
    
    namespace Unmanaged
    {
    	public delegate uint LPTHREAD_START_ROUTINE(uint lpParam);
    
    	class Class1
    	{
    		public const uint INFINITE = 0xFFFFFFFF;
    
    		[DllImport("Kernel32.dll")]
    		public static extern  uint CreateThread(
    			uint lpThreadAttributes,
    			uint dwStackSize,
    			LPTHREAD_START_ROUTINE lpStartAddress,
    			uint lpParameter,
    			uint dwCreationFlags,
    			out uint lpThreadId);
    
    		[DllImport("Kernel32.dll")]
    		public static extern int CloseHandle(uint hObject);
    
    		[DllImport("Kernel32.dll")]
    		public static extern uint WaitForSingleObject(uint hHandle, uint dwMilliseconds);
    
    		[DllImport("Kernel32.dll")]
    		public static extern void Sleep(uint dwMilliseconds);
    
    		[STAThread]
    		static void Main(string[] args)
    		{
    			uint dwThread;
    			LPTHREAD_START_ROUTINE lpThread = new LPTHREAD_START_ROUTINE(Class1.ThreadProc);
    			uint hThreadHandle = CreateThread(0, 0, lpThread, 0, 0, out dwThread);
    
    			WaitForSingleObject(hThreadHandle, INFINITE);
    			CloseHandle(hThreadHandle);
    
    			Console.WriteLine("That's end... Press enter... ");
    			Console.ReadLine();
    		}
    
    		public static uint ThreadProc(uint lpParam)
    		{
    			for (int i = 0; i < 20; i++)
    			{
    				Console.WriteLine("Thread output #{0}", i.ToString());
    				Sleep(100);
    			}
    			return 0;
    		}
    	}
    }
    Martin

  3. #3
    Join Date
    Apr 2003
    Posts
    18

    Thank you

    You are right about the documentation, but the teacher didn't give any, and i didn't find something like that in MSDN. I could have done it easyly in VC++ 6 like every other student, but he wanted me to solve the poblem in c#. Thanks a lot.

  4. #4
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    Don't say me, that there is nothing as this in MSDN!!! Look into the DllImport attribute documentation then to whole topic of "Inteoperating with Unmanaged Code" and especially at "Consuming Unmanaged DLL Functions".

    There is everything you need to create and understand such snippet of code...

    Martin

  5. #5
    Join Date
    Apr 2003
    Posts
    18

    Thank you again

    I didn's say there is nothing like this in MSDN. I just said i didn't find anything. Since i am very occupied with my job, i didn't have much time to search. Thank you again for your advice.
    Regards,
    Cris

  6. #6
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    as Martin said you will need to either search MSDN. there is a lot info regarding doing this stuff including samples, the magic word that you use for search should be the right word which will help you in searching the right documentation
    - Software Architect

  7. #7
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    Yes Paresh... The searching...

    That is a point of all probles... Any one starts searching for some information (I am not talking just about the MSDN. It is general - for example searching through google.com) and after a couple of tries he just says "where the **** those information are ?! There is nothing about the stuff I am searching for". Everybody is grumbling about the stupidity of search engines or even about the shortness of the documentation (MSDN). However, everytime I had searched something, I found it. The difference between my searches and searches done by people which I am talking about is in the search phrase used for searching. I am not able to describe how I build those phrases, however they work.

    Many people (my colleagues) are asking me how they should write those phrases. I don't know how to answer them... I just say them, that the search phrase has to describe the problem they are searching solution for in the simplest way it is possible. They have to include just relevant information about the stuff they are searching for. They should not use phrases like "how to ... ".
    For example: if you are looking for the stuff discussed in this thread, don't use question like:
    "how to call dll functions",
    because this question gives you 54 topics in .Net Framework SDK Documentation. If you specify the question as follows:
    "unmanaged dll functions callback"
    it gives you just 8 topics and the first one is interesting for us...

    I don't know how to describe the method of building these phrases generally.

    What is the reason that people use stupid search phrases? That they don't know how to search information? I cannot believe it is just their stupidity...

    martin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured