Click to See Complete Forum and Search --> : HANDLE in a managed project


NiKLai
January 29th, 2007, 10:44 AM
Hi,

I'm having problem to declare a HANDLE in my project.

Actually, it's a C++ managed project (form based) in Visual Studio 2005. However, I need to deal with unmanaged C++ such as handles, events via SetEvents and CreateEvents, etc.

What I did is to include a new class in my project. I didn't use the keyword ref when I declared that class. Instead I simply used "public".

After that, I included <windows.h> in the .cpp of my class to get access to the desired function. I did also include <winnt.h> and <Kernel32.lib> to make sure every definitions I needed would be declared.

Well...when declaring a HANDLE, or when using SetEvent or CreateEvent, the compiler raise an error saying that those are undeclared identifiers.

What is strange, it's that the intellisense shows me these types as if everything would be ok.

Does anyone know how I can resolve my problem?
Thanks.

Nicolas

NiKLai
January 29th, 2007, 01:34 PM
I attached two screenshots which demonstrate my problem. (Don't know if it will work, it's the first time I atempt to attach a file in this forum)

Hope someone around here has a solution...

Zaccheus
January 29th, 2007, 02:31 PM
You included <Kernel32.lib> ? :confused:

Please post your code so we can see what is wrong.
:)

NiKLai
January 29th, 2007, 03:31 PM
Yeah I've included Kernel32.lib just because I was totally desperate and didn't know what else to try. :)

I can't post my entire code here. My company is really paranoid with confidentiality and industrial secrets... so I prefer not take a chance.

Bue I've finally found a way to resolve my problem. I create a new project, Win32 DLL.

My code compile now and everything seems to be alright.

Thanks for trying to help me ;-)

James2007
January 29th, 2007, 03:42 PM
In 'StdAfx.h' put this line....


#include <windows.h>


And the safe type for HANDLE in CLI is System::IntPtr.

So, instead you could put....



using namespace System;
using namespace System::Runtime::InteropServices;

namespace WindowsAPI
{
[DllImport("Kernel32.dll", EntryPoint = "CreateEventW", CallingConvention = CallingConvention::StdCall, CharSet = CharSet::Unicode)] System::IntPtr CreateEvent(System::IntPtr lpSecurity,int nManualReset,int nInitialState,System::String ^ strName);
[DllImport("Kernel32.dll", EntryPoint = "CloseHandle", CallingConvention = CallingConvention::StdCall, CharSet = CharSet::Unicode)] int CloseHandle(System::IntPtr handle);
[DllImport("Kernel32.dll", EntryPoint = "SetEvent", CallingConvention = CallingConvention::StdCall, CharSet = CharSet::Unicode)] int SetEvent(System::IntPtr handle);
[DllImport("Kernel32.dll", EntryPoint = "ResetEvent", CallingConvention = CallingConvention::StdCall, CharSet = CharSet::Unicode)] int ResetEvent(System::IntPtr handle);

public ref class Test
{
public:
Test()
{
Handle = CreateEvent(System::IntPtr(0),1,0,"");
}
virtual ~Test()
{
CloseHandle(Handle);
}
private:
System::IntPtr Handle;
};
}

Zaccheus
January 29th, 2007, 04:12 PM
I can't post my entire code here. My company is really paranoid with confidentiality and industrial secrets... so I prefer not take a chance.
Good thinking.
:)

NiKLai
January 30th, 2007, 12:08 PM
Thanks James for the help! :wave: