Libcurl error in a Windows Forms Application in Visual C++ 2010 express edition - libcurl

I have used libcurl in win32 console application and it is working fine. I have followed instruction from here. http://theetrain.ca/tech/installing-curl-using-visual-studio-2010-beginners-guide/. Now I want to use Lib curl in Windows Forms Application. But it is showing some errors.
1>Read website app.obj : error LNK2031: unable to generate p/invoke for "extern "C" void * __clrcall curl_easy_init(void)" (?curl_easy_init##$$J0YMPAXXZ); calling convention missing in metadata
1>Read website app.obj : error LNK2028: unresolved token (0A000011) "extern "C" void * __clrcall curl_easy_init(void)" (?curl_easy_init##$$J0YMPAXXZ) referenced in function "private: void __clrcall Readwebsiteapp::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click#Form1#Readwebsiteapp##$$FA$AAMXP$AAVObject#System##P$AAVEventArgs#4##Z)
1>Read website app.obj : error LNK2019: unresolved external symbol "extern "C" void * __clrcall curl_easy_init(void)" (?curl_easy_init##$$J0YMPAXXZ) referenced in function "private: void __clrcall Readwebsiteapp::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click#Form1#Readwebsiteapp##$$FA$AAMXP$AAVObject#System##P$AAVEventArgs#4##Z)
1>C:\Documents and Settings\nemo1\my documents\visual studio 2010\Projects\Readwebsiteapp\Debug\Read website app.exe : fatal error LNK1120: 2 unresolved externals
Please give me some advise to solve this problem. Thank you. My code is:
#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string.h>
using namespace std;
#pragma once
namespace Readwebsiteapp {
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: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::TextBox^ textBox1;
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->button1 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(120, 70);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(120, 153);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 20);
this->textBox1->TabIndex = 1;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(420, 337);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
textBox1->Text = "hello";
CURL *curl;
CURLcode result;
curl = curl_easy_init();
}
};
}

Since you're compiling managed C++, your native curl calls need to be marshaled through p/Invoke. It might be easiest to wrap these in flat "C" calls to make the marshaling simpler. In any case, I'd use the P/Invoke Interop Assistant tool (http://clrinterop.codeplex.com/releases/view/14120) to generate the necessary signatures.

Related

visual studio c++ forms multiple inclusion

I know there is a lot of posts about this topic, but still I can't get it to work.
I created a new project that called "nis".
This is "nis.cpp":
// nis.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace nis;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
I have two forms.
This is "Form1.h":
#ifndef FORM1_H
#define FORM1_H
#pragma once
#include "Form2.h"
namespace nis {
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: System::Windows::Forms::Button^ button1;
protected:
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->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(78, 77);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(144, 53);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(282, 253);
this->Controls->Add(this->button1);
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) {
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->Hide();
Form2^ f1 = gcnew Form2();
f1->ShowDialog();
}
};
}
#endif
And this is "Form2.h":
#ifndef FORM2_H
#define FORM2_H
#pragma once
#include "Form1.h"
namespace nis {
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 Form2
/// </summary>
public ref class Form2 : public System::Windows::Forms::Form
{
public:
Form2(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form2()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
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->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(44, 102);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(149, 53);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
//
// Form2
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(282, 253);
this->Controls->Add(this->button1);
this->Name = L"Form2";
this->Text = L"Form2";
this->Load += gcnew System::EventHandler(this, &Form2::Form2_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form2_Load(System::Object^ sender, System::EventArgs^ e) {
}
};
}
#endif
Can you please tell me what's wrong with the inclusion guards?
I get this errors:
Error 1 error C2065: 'Form2' : undeclared identifier
d:\users\lior\documents\visual studio 2010\projects\nis\nis\Form1.h 85
Error 2 error C2065: 'f1' : undeclared identifier
d:\users\lior\documents\visual studio 2010\projects\nis\nis\Form1.h 85
Error 3 error C2061: syntax error : identifier 'Form2'
d:\users\lior\documents\visual studio 2010\projects\nis\nis\Form1.h 85
Error 4 error C2065: 'f1' : undeclared identifier
d:\users\lior\documents\visual studio 2010\projects\nis\nis\Form1.h 86
Error 5 error C2227: left of '->ShowDialog' must point to
class/struct/union/generic type d:\users\lior\documents\visual studio
2010\projects\nis\nis\Form1.h 86
Thank you so much!
You include form 1 in form 2 and form 2 in form 1. What do you expect the preprocessor to do? You'll have to make a choice here! In general, modules should form a hierarchy rather than a network, and certainly not a graph with cycles, which would mean that a module in the end includes itself.
If you want to mutually refer between form 1 and form 2, don't put all your code in headers but make separate implementation files. Your headers will hold only your class decls. The accompanying C++ files will hold the code of the methods, including their refs to the other form.

C++ Visual Studio 2015 CLR Windows Form Namespace error

Hi I'am trying to create a function that want to take in the class for the Form thats created with the Windows Form Wizard. The Visual Studio is using the project name as a namespace to the Form class 'NewCustomerForm' but whene I try to create a function with the class as an argument it says whene I compile the code I get this error message:
Error C2871 'Project1': a namespace with this name does not exist
This is the file it complain on NewCustomerTab.h:
#ifndef __NewCustomerTab_H__
#define __NewCustomerTab_H__
#include "NewCustomerForm.h"
using namespace Project1;
ref class CNewCustomerTab :
public System::Windows::Forms::TabPage{
public:
CNewCustomerTab(NewCustomerForm^);
private:
System::Windows::Forms::Form^ form1;
};
And this is the Form class NewCustomerForm.h:
#pragma once
#include "NewCustomerTab.h"
namespace Project1 {
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 NewCustomerForm
/// </summary>
public ref class NewCustomerForm : public Form
{
public:
NewCustomerForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~NewCustomerForm()
{
if (components)
{
delete components;
}
}
public: System::Windows::Forms::Panel^ panel1;
private: System::Windows::Forms::Label^ label1;
protected:
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->panel1 = (gcnew System::Windows::Forms::Panel());
this->label1 = (gcnew System::Windows::Forms::Label());
this->panel1->SuspendLayout();
this->SuspendLayout();
//
// panel1
//
this->panel1->Controls->Add(this->label1);
this->panel1->Dock = System::Windows::Forms::DockStyle::Fill;
this->panel1->Location = System::Drawing::Point(0, 0);
this->panel1->Name = L"panel1";
this->panel1->Size = System::Drawing::Size(842, 563);
this->panel1->TabIndex = 0;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 24, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->Location = System::Drawing::Point(251, 31);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(229, 37);
this->label1->TabIndex = 0;
this->label1->Text = L"New Customer";
//
// NewCustomerForm
//
//this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
//this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(842, 563);
this->Controls->Add(this->panel1);
this->Name = L"NewCustomerForm";
this->Text = L"NewCustomerForm";
this->panel1->ResumeLayout(false);
this->panel1->PerformLayout();
this->ResumeLayout(false);
}
#pragma endregion
};
}
What do I do wrong I include the Form class file and there it says that Project1 is a namespace.
You must compile with the Common Language Runtime Support (/clr).
Rigth click on your solution, in configuration properties, select general, and select clr for Common Language Runtime Support.

Changing Textbox from the cpp file

What I'm trying to do is change a textbox from the form1 from within the main '.cpp' file.
Basically, I want the text field to change on load. (Later it will be another reason).
Sorry if this is a dumb question. I'm a newbie.
I added the 2 lines in the Header file and changed the textbox from private to public. (guessing this isn't a good idea?)
Then I tried calling the change in the main function of the .cpp file.
Thank you so much for your help
.cpp file
// help.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace help;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
//Added this.
Form1^ myform1 = gcnew Form1();
Form1::myForm1->MyBox->Text = L" ShowME! ";
return 0;
}
form1.h file
#pragma once
namespace help {
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:
static Form1^ myForm1; //Add this...
Form1(void)
{
InitializeComponent();
myForm1 = this; //added this...
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
public: System::Windows::Forms::TextBox^ MyBox; //Changed To Public..
protected:
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->MyBox = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// MyBox
//
this->MyBox->Location = System::Drawing::Point(20, 57);
this->MyBox->Name = L"MyBox";
this->MyBox->Size = System::Drawing::Size(247, 20);
this->MyBox->TabIndex = 0;
this->MyBox->TextChanged += gcnew System::EventHandler(this, &Form1::MyBox_TextChanged);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Controls->Add(this->MyBox);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void MyBox_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
};
}
Update....
I'm still trying to figure it out. Today I woke up early to try again and I can't seem to get it to work.
I tried making a function called "go" on From1.h and then running the function from the cpp to see if it changes the box and nothing.
So then I decided to see if the function is running at all... Then I wrote a file and put hello in that file using the same function that suppose to update the textbox. It wrote the file no problem but the textbox is still unchanged Please Help!
cpp file
#include "stdafx.h"
#include "Form1.h"
using namespace test6;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Form1 peaches;
peaches.go();
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
Form1.h
#include <iostream>
#include <sstream>
#include <String>
#pragma once
namespace test6 {
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
//
}
void go() {
//This doesn't update the Box?
MyBox->Text = ("Hello");
//This does make a file with hello in it.
FILE * fname = fopen("text.txt","w");
fprintf(fname, "Hello");
fclose(fname);
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ MyBox;
private: System::Windows::Forms::Button^ button1;
protected:
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->MyBox = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// MyBox
//
this->MyBox->Location = System::Drawing::Point(99, 112);
this->MyBox->Name = L"MyBox";
this->MyBox->Size = System::Drawing::Size(100, 20);
this->MyBox->TabIndex = 0;
this->MyBox->TextChanged += gcnew System::EventHandler(this, &Form1::MyBox_TextChanged);
//
// button1
//
this->button1->Location = System::Drawing::Point(110, 178);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Controls->Add(this->button1);
this->Controls->Add(this->MyBox);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void MyBox_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
};
}
you can do this my setting the textbox text on the constructor of the form.
Form1(void)
{
InitializeComponent();
myForm1 = this; //added this... (?? no need)
//
//TODO: Add the constructor code here
MyBox.Text = "Result is 22"; //where MyBox is your text box object.
}
Note that :
No need to make text box public, as private members can be access within the class without any issue. (text box is defined inside the 'Form class' )
Update to your second question:
Form1 peaches; // obj1
peaches.go();
// Create the main window and run it
Application::Run(gcnew Form1()); // obj2
Look at your code, carefully. You have two distinct Form1 objects.
1. At peaches (obj1) you call go) function. Nothing happen because your UI not running at that moment.
Then you create another object with gcnew Form1() and lunching the UI. No change in UI, because you call go() in the other object.
Try this,
Form1 ^peaches = gcnew Form1();
// Create the main window and run it
Application::Run(peaches);
peaches->go();

How can I use a WindowsFormApplication button to toggle between two Windows applications (force them to top)?

I have the following objective:
Create a windows button that a) on a click switches its label and enforces a running windows application (e.g., Calculator) to the front and b) on a subsequent click changes its label back to the original state and forces a second windows application (e.g., Notepad) to the front. This toggling should be possible to continue infinite times (until user aborts).
Note: I am using MS Visual Studio.
What I have so far (built based on various other threads):
1) A successfully running WindowsFormApplication providing a button that upon a clicking it, toggles its label from "Calculator" to "Notepad" and vice versa.
2) A Win32 Console Application that successfully finds the running Calucalor application and enforces it to the front.
Code for 1)
ButtonSwitchApplication.cpp
// ButtonSwitchApplication.cpp : main project file.
#include "Form1.h"
#include <windows.h>
using namespace ButtonSwitchApplication;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
//HelloWorld();
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
Form1.h
#pragma once
#include <windows.h>
namespace ButtonSwitchApplication {
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: System::Windows::Forms::Button^ button1;
protected:
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->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(26, 45);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(232, 53);
this->button1->TabIndex = 0;
this->button1->Text = L"INFO"; // initializes the button label with "INFO"
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1::button1_Click(System::Object^ sender, System::EventArgs^ e) {
static bool isInfo = true;
if(isInfo == false)
{
button1->Text = "Calculator";
} // end if (isInfo)
else
{
button1->Text = "Notepad";
} // end else
isInfo = !isInfo;
} // end private button1_Click
};
}
Code for 2)
Source.cpp
#include <iostream> // need this header file to support the C++ I/O system
#include <windows.h>
using namespace std; // telling the compiler to use namespace "std",
// where the entire C++ library is declared.
int main()
{
// This code is searching for a running application
// Retrieves a handle to the top-level window whose class name and window name match the specified strings.
// This function does not search child windows.
// This function does not perform a case-sensitive search.
HWND hwnd1 = FindWindow(NULL,TEXT("Calculator"));
HWND hwnd2 = FindWindow(NULL,TEXT("Notepad"));
// used temporarily to decide which window to search for
int MyTemp = 0;
if (MyTemp == 0) {
SetForegroundWindow(hwnd1);
cout << " Found Calculator!" << endl;
cout << hwnd1 << endl;
getchar();
} // end if
else if (MyTemp == 1) {
SetForegroundWindow(hwnd2);
cout << " Found Notepad!" << endl;
cout << hwnd2 << endl;
getchar();
} // end else if
else {
cout << " Did not find the application window!" << endl;
cout << hwnd1 << endl;
getchar();
}
}
I am unsuccessful with to marrying 1) and 2) two together so that the above stated objective is met.
I tried to include
HWND hwnd = FindWindow(NULL,TEXT("Calculator"));
SetForegroundWindow(hwnd);
MessageBox::Show("Found Calculator!");
right after
button1->Text = "Calculator";
in Form1.h, but I receive errors like:
1> ButtonSwitchApplication.cpp
1>ButtonSwitchApplication.obj : error LNK2028: unresolved token (0A000019) "extern "C" int __stdcall SetForegroundWindow(struct HWND__ *)" [...]
1>ButtonSwitchApplication.obj : error LNK2028: unresolved token (0A00001E) "extern "C" struct HWND__ * __stdcall FindWindowW(wchar_t const [...]
1>ButtonSwitchApplication.obj : error LNK2019: unresolved external symbol "extern "C" struct HWND__ * __stdcall FindWindowW(wchar_t const [...]
1>ButtonSwitchApplication.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall SetForegroundWindow(struct HWND__ *)" [...]
Any help on how to accomplish the objective is much appreciated.
Thanks!
Best,
Michael.
I figured it out myself ... after a long time:
A couple of things were key to success:
using the right namespace for the SetForegroundWinow functions
including user32.lib in Properties>Configuration Properties>Linker>Input>Additional Dependencies
Here is the running code that gives me the toggle button that brings two running applications to the foreground (in this example the calculator and the notepad)
ButtonSwitchApplication.cpp:
// ButtonSwitchApplication.cpp : main project file.
#include <iostream> // need this header file to support the C++ I/O system
#include <windows.h>
//define the functions that do the work
namespace ButtonSwitchApplication
{
using namespace System::Windows::Forms;
System::Void CalcToTop(){
// This code is searching for a running application
// Retrieves a handle to the top-level window whose class name and window name match the specified strings.
// This function does not search child windows.
// This function does not perform a case-sensitive search.
HWND hwnd = FindWindow(NULL,TEXT("Untitled - Notepad"));
SetForegroundWindow(hwnd);
//MessageBox::Show("Found Notepad!");
} // end CalcToTop
System::Void NotepadToTop(){
// This code is searching for a running application
// Retrieves a handle to the top-level window whose class name and window name match the specified strings.
// This function does not search child windows.
// This function does not perform a case-sensitive search.
HWND hwnd = FindWindow(NULL,TEXT("Calculator"));
SetForegroundWindow(hwnd);
//MessageBox::Show("Found Calculator !");
} // end CalcToTop
}
// includes all of the VS generated forms code
#include "Form1.h"
using namespace ButtonSwitchApplication;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
Form1.h:
#pragma once
#include <windows.h>
namespace ButtonSwitchApplication {
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: System::Windows::Forms::Button^ button1;
protected:
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->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->AutoSizeMode = System::Windows::Forms::AutoSizeMode::GrowAndShrink;
this->button1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button1->Dock = System::Windows::Forms::DockStyle::Fill;
this->button1->Location = System::Drawing::Point(0, 0);
this->button1->MinimumSize = System::Drawing::Size(200, 60);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(345, 86);
this->button1->TabIndex = 0;
this->button1->Text = L"CALCULATOR";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(345, 86);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1::button1_Click(System::Object^ sender, System::EventArgs^ e) {
static bool isCalc = true;
if(isCalc == false)
{
button1->Text = "CALCULATOR";
ButtonSwitchApplication::CalcToTop();
} // end if (isInfo)
else
{
button1->Text = "NOTEPAD";
ButtonSwitchApplication::NotepadToTop();
} // end else
isCalc = !isCalc;
} // end private button1_Click
};
}

Visual C++ Forms Application won't run on other computers

I'm very new to C++ and I am trying to create a simple Forms Application with Visual C++ Express 2010. I got it to compile with no errors when it was just a header file and a .cpp file and it ran on my computer perfectly fine. When I tried to give my friend the compiled .exe to run, it didn't run. He had the .NET framework 4.5 and the Visual C++ 2010 redistributable package and it still refused to work. He says it just won't start up at all(I don't know if it gives him an error message). All the program had was a button that changed a label. I'm tearing my hair out here because I can't seem to get this to work on any other computer but my own. Please help me understand why this will not run on another computer. It could be an error I made, so the code is down below. As you can see, I've turned it into 3 .cpp files to see if that would work. There's alot of pointless code here, but it still compiles fine.
//Source2.cpp
#pragma once
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 Header1 : public System::Windows::Forms::Form
{
public:
Header1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Header1()
{
if (components)
{
delete components;
}
}
public: System::Windows::Forms::Button^ button1;
System::Windows::Forms::Label^ label1;
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->button1 = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(13, 13);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Header1::button1_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(13, 43);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(46, 17);
this->label1->TabIndex = 1;
this->label1->Text = L"label1";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(282, 253);
this->Controls->Add(this->label1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->label1->Text = "works";
}
};
^^Note that this was originally a header file called Header1.h
//Source1.cpp
#include "stdafx.h"
#include "Source2.cpp"//used to include Header1.h
public class runpr
{
public:
runpr()
{
create();
}
~runpr();
private:
void create()
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Header1());
//^^all of this was originally in the main function
}
};
This file is pretty pointless because all of what it does could just be done in the main function or the class could even be in the main project file. I just tried it to see if it would work, but to no avail.
// Testing123.cpp : main project file.
#include "stdafx.h"
#include "Source1.cpp"
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
runpr* start = new runpr;
//creates an instance of runpr class
return 0;
}
Again, any help would be appreciated. Sorry if this was too long and stupid of a question
The Forms Application must be compiled in release mode, since the VC++ Redistributable Package only includes the release dlls ("Debug versions of an application are not redistributable, and debug versions of the Visual C++ library DLLs are not redistributable" Redistributing Visual C++ Files).