Windows Form Apps

how to call windows.h functions within a header file (basically i created an app using windows form application...and i need to call windows.h function when hitting certain buttons)
I'm not sure what your question involves -- if you #include <windows.h> in your application, then you can call Windows functions anywhere in your program.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "stdafx.h"
#pragma once

namespace WindowsFormsApplication1 {
	
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->SuspendLayout();
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(284, 262);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
				
			 }
	};
}

ok i want to do:
1
2
3
4
5
6
7
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
				/*i want to call RegOpenKeyValue() for example 
				i cant do it directly it wont work*/
			 }
	};
}
There is no such thing like RegOpenKeyValue(), maybe you want RegOpenmKeyEx() ?
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724897(v=vs.85).aspx
yes ...thank you i mistook it....how can i call it inside Form1_Load(...) , if i did something like this:
1
2
3
4
5
6
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
				RegOpenKeyEx( hKey, lpSubKey, ulOptions,samDesired, phkResult);
			 }
	};
}

it pops:
1>WindowsFormsApplication1.obj : error LNK2028: unresolved token (0A00000D) "extern "C" long __stdcall RegOpenKeyExW(struct HKEY__ *,wchar_t const *,unsigned long,unsigned long,struct HKEY__ * *)" (?RegOpenKeyExW@@$$J220YGJPAUHKEY__@@PB_WKKPAPAU1@@Z) referenced in function "private: void __clrcall WindowsFormsApplication1::Form1::Form1_Load(class System::Object ^,class System::EventArgs ^)" (?Form1_Load@Form1@WindowsFormsApplication1@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
Last edited on
Have you done this? :

1
2
3
4
#include <windows.h>
#include "stdafx.h"

//(the rest of your program code goes here) 


Also, make sure that Advapi32.lib is added in your Project Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies. This .lib is required for using RegOpenmKeyEx().

EDIT:

If you look at the bottom of the webpage linked by modoran, you'll see that the MSDN documentation for RegOpenmKeyEx() mentions:
Requirements:

Minimum supported client:
Windows 2000 Professional [desktop apps only]

Minimum supported server:
Windows 2000 Server [desktop apps only]

Header:
Winreg.h (include Windows.h)

Library:
Advapi32.lib

DLL:
Advapi32.dll

Unicode and ANSI names:
RegOpenKeyExW (Unicode) and RegOpenKeyExA (ANSI)


That's how you can determine the requirements for a function to work -- this one needs Advapi32.lib to work (and of course including windows.h).
Last edited on
yes i considered that .... even if you changed this function to any other function it wont work...i tried GetModuleHandle() and FindWindow() ....the same error pops out....try it you will see....my purpose is to know why the previous function(or Windows.h functions) cannot be invoked??
Last edited on
my purpose is to know why the previous function(or Windows.h functions) cannot be invoked??

You are mixing Managed (.net framework) code with unmanaged (native) code.

I would suggest that, you don't know the difference, you spend a little time looking at the two 'ways' of programming windows and make sure you are on the right track for what you want to learn.
Grey Wolf has a good suggestion, you have to decide which path you're going down. if you choose managed: this will help
This is your namespace directive. I put it last.
 
using namespace Microsoft::Win32;

No additional includes are necessary
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
RegistryKey^ currentUser;
	RegistryKey^ softwarekey;
	currentUser = Registry::CurrentUser;
	softwarekey = currentUser->OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer");
	String^ regVal = softwarekey->GetValue("Browse For Folder Height")->ToString();

	MessageBox::Show(regVal);
	if(currentUser)
	{
		currentUser->Close();
	}
	if(softwarekey)
	{
		softwarekey->Close();
	}

Good luck.
thanks a lot you all....i didnt know those are totally different ....the last question
Ok what is this operator suppose to mean ^...as i know it is supposed to be used in bitwise operation .....in this
1
2
RegistryKey^
RegistryKey  //what is the difference?? 
Last edited on
You are using an entirely different language then C++, so there are different language syntax and rules. Are sure you want to use Microsoft C++/CLI ?

As for your question, read this:
http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/0a4bdea7-6d86-4e11-85d9-3884907ff962/

That forum is also the right place for that language, as here not much users know it.
ok thanks a lot
Topic archived. No new replies allowed.