problem with GetDC()

hi...
i have a litle project witch is C++/CLR empty + added cpp file + UI file (Form)

i want to do a single button when clicked does this code..


in my btn click event :

private: System::Void Button1_Click(System::Object^ sender, System::EventArgs^ e) {


HDC test;
test = GetDC(NULL);
}



but when building . it give me this error :

-1>MyForm.obj : error LNK2028: unresolved token (0A00003A) "extern "C" struct HDC__ * __stdcall GetDC(struct HWND__ *)"
(?GetDC@@$$J14YGPAUHDC__@@PAUHWND__@@@Z) referenced in function "private: void __clrcall CFORMS::MyForm::Button1_Click(class System::Object ^,class System::EventArgs ^)" (?Button1_Click@MyForm@CFORMS@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)

-1>MyForm.obj : error LNK2019: unresolved external symbol "extern "C" struct HDC__ * __stdcall GetDC(struct HWND__ *)" (?GetDC@@$$J14YGPAUHDC__@@PAUHWND__@@@Z) referenced in function "private: void __clrcall CFORMS::MyForm::Button1_Click(class System::Object ^,class System::EventArgs ^)" (?Button1_Click@MyForm@CFORMS@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)

-1>C:\Users\HB\Documents\Staff\C++ FORMS\Debug\C++ FORMS.exe : fatal error LNK1120: 2 unresolved externals

my entire code :

-----------------------------------------------------------------------
[[ File : MyForm.cpp ]]
+++++++++++++++++++++++

#include "MyForm.h"

using namespace System;

[STAThreadAttribute]
int main()
{
Windows::Forms::Application::Run(gcnew CFORMS::MyForm());
return 0;
}

----------------------------------------------------------------------
[[ File : MyForm.h ]]
+++++++++++++++++++++

#pragma once

#include<Windows.h>

namespace CFORMS {

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 MyForm
/// </summary>

public ref class MyForm : public System::Windows::Forms::Form
{

public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::PictureBox^ pictureBox1;
protected:
private: System::Windows::Forms::PictureBox^ pictureBox2;
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->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
this->pictureBox2 = (gcnew System::Windows::Forms::PictureBox());
this->button1 = (gcnew System::Windows::Forms::Button());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox2))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->BackColor = System::Drawing::Color::Red;
this->pictureBox1->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
this->pictureBox1->Location = System::Drawing::Point(230, 158);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(96, 106);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// pictureBox2
//
this->pictureBox2->BackColor = System::Drawing::SystemColors::HotTrack;
this->pictureBox2->Location = System::Drawing::Point(164, 12);
this->pictureBox2->Name = L"pictureBox2";
this->pictureBox2->Size = System::Drawing::Size(162, 106);
this->pictureBox2->TabIndex = 1;
this->pictureBox2->TabStop = false;
//
// button1
//
this->button1->Location = System::Drawing::Point(52, 225);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 2;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &MyForm::Button1_Click);
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::SystemColors::ControlLightLight;
this->ClientSize = System::Drawing::Size(484, 399);
this->Controls->Add(this->button1);
this->Controls->Add(this->pictureBox1);
this->Controls->Add(this->pictureBox2);
this->Cursor = System::Windows::Forms::Cursors::Cross;
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
this->Name = L"MyForm";
this->Text = L"MyForm";
this->TopMost = true;
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox2))->EndInit();
this->ResumeLayout(false);

}
#pragma endregion

private: System::Void Button1_Click(System::Object^ sender, System::EventArgs^ e) {


HDC test;
test = GetDC(NULL);
}
};
}

------------end-----------------------
I don't know how NET framework works, but you are getting linker errors because you didn't link against required libraries.

You need to link against one or more of the bellow libraries:
user32.lib
gdi32.lib
kernel32.lib

Solution found

special thanks to malibor

i added those lines under ...#include<windows.h>

1
2
3
4
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "comdlg32.lib") 

Last edited on
Registered users can post here. Sign in or register to post.