Opening forms show function. Visual Studio c++ - forms

I have main window.
main.cpp
#include "mainWindow.h"
using namespace System;
using namespace System::Windows::Forms;
int main()
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Client::mainWindow mainWin;
Application::Run(%mainWin);
return 0;
}
And i have another form, which should open when the main form openning:
mainWindow.h
private: System::Void mainWindow_Shown(System::Object^ sender, System::EventArgs^ e) {
Client::logForm^ myForm = gcnew logForm();
myForm->Show();
//Client::logForm logF;
//logF.Show();
}
And my question: why using the commented code, the form opens and closes immediately, but uncommented code works good?
Thank you!

Related

How to debug dll (Unity)?

I have a Unity project that use dll.
This is an example I try to use : https://www.youtube.com/watch?v=C6V1f86x058
In order to make it work I :
created C# script
Added dll under Assets folder
My script is
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Rendering;
public class TestScript : MonoBehaviour
{
//the name of the DLL you want to load stuff from
private const string pluginName = "AndroidNativeLib";
//native interface
[DllImport(pluginName)]
private static extern IntPtr getEventFunction();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void DebugDelegate(string str);
static void CallBackFunction(string str) { Debug.Log(str); }
[DllImport(pluginName)]
public static extern void SetDebugFunction(IntPtr fp);
private CommandBuffer cmd;
// Start is called before the first frame update
void Start()
{
DebugDelegate callback_delegate = new DebugDelegate(CallBackFunction);
// Convert callback_delegate into a function pointer that can be
// used in unmanaged code.
IntPtr intptr_delegate =
Marshal.GetFunctionPointerForDelegate(callback_delegate);
// Call the API passing along the function pointer.
SetDebugFunction(intptr_delegate);
//crating the command buffer and attaching it to camera
cmd = new CommandBuffer();
cmd.name = pluginName;
var camera = Camera.main;
camera.AddCommandBuffer(CameraEvent.AfterGBuffer, cmd);
}
// Update is called once per frame
void Update()
{
cmd.IssuePluginEvent(getEventFunction(), 0);
}
}
And my dll is :
#include "stdafx.h"
#include "IUnityGraphics.h"
//make sure this appears before IUnityGraphicsD3D11
#include "d3d11.h"
#include "IUnityGraphicsD3D11.h"
// debug event
typedef void(*FuncPtr)(const char *);
FuncPtr Debug;
namespace globals {
ID3D11Device *device = nullptr;
ID3D11DeviceContext *context = nullptr;
} // namespace globals
extern "C" {
UNITY_INTERFACE_EXPORT void SetDebugFunction(FuncPtr fp) { Debug = fp; }
// Plugin function to handle a specific rendering event
static void UNITY_INTERFACE_API UNITY_INTERFACE_API OnRenderEvent(int eventID) {
Debug("Hello world");
}
// Unity plugin load event
void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
UnityPluginLoad(IUnityInterfaces *unityInterfaces) {
auto s_UnityInterfaces = unityInterfaces;
IUnityGraphicsD3D11 *d3d11 = unityInterfaces->Get<IUnityGraphicsD3D11>();
globals::device = d3d11->GetDevice();
globals::device->GetImmediateContext(&globals::context);
}
// Unity plugin unload event
void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload() {}
// Freely defined function to pass a callback to plugin-specific scripts
UnityRenderingEvent UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
getEventFunction() {
return OnRenderEvent;
}
}
When I run this example I don't see any outputs and it does not looks like it work. So, I tried to debug it. I can get debug point in C# script, but if I try to reach one from dll code, noting happens.
So, question is - how to debug dll code?
Does this help: https://forum.unity.com/threads/how-to-build-and-debug-external-dlls.161685/
create a .csproj file that uses wildcards to find all source code
add this C# project to a Visual Studio solution
use an MSBuild post-build target to convert Visual Studio's PDB symbols to Mono's MDB format
debug with MonoDevelop from Unity, as normal
source code should -not- be in the Assets folder, but the generated DLL should be

Open NotePad in mys form on top

Can you hel me please to open an application (notepad) on top my form ?
private void button1_Click(object sender, EventArgs e)
{
Process N = new Process.Start(#"notepad.exe");
N.ShowDialog(this);
}
I try this but is not correctly.
So, how can i open notepad.exe or word.exe on top my form as ShowDialog please ?
Cordialy
Use SetForegroundWindow function.
Declare using System.Runtime.InteropServices; and use
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
private void SetNotepadAsForegroundApp()
{
Process N = new Process.Start(#"notepad.exe");
SetForegroundWindow(N.MainWindowHandle);
}

Opening a form C++ from 2 different forms

I have 3 forms:
MainForm
ManualForm
AutomaticForm
I have a button MainForm-> ManualForm:
private: System::Void btnPowerOn_Click(System::Object^ sender, System::EventArgs^ e)
{
ManualForm^ form = gcnew ManualForm();
form->ShowDialog();
}
I also have a button for AutomaticForm -> ManualForm:
private: System::Void btnManual_Click(System::Object^ sender, System::EventArgs^ e)
{
this->Close();
ManualForm^ manForm = gcnew ManualForm();
manForm->ShowDialog();
}
but when I added this second button it says:
syntax error: identifier 'ManualForm'. (in the AutomaticForm.h)
In both MainForm and AutomaticForm I have the #include "ManualForm.h".
So how can I solve this to let my buttons do their job? (switching between forms)
Ok it seems it is a problem with including each other.
I simple solved it by not closing eachother and keeping the previous Form open.
So for each button I just do the following:
private: System::Void btn_Automatic_Click(System::Object^ sender, System::EventArgs^ e)
{
//this->Close(); removed this line so I don't have to reopen it again
AutomaticForm^ aForm = gcnew AutomaticForm();
aForm->ShowDialog();
}
and when I want to go back to the manualForm, I just simply do this:
private: System::Void btn_Manual_Click(System::Object^ sender, System::EventArgs^ e)
{
this->Close();
}

Static and non-static method or global class object

I'm making a Windows Forms Application in VS2012 C++.
Situation just for example, real project is more complicated:
I have a Form that contains TextBox, Button and Timer.
Button just triggers the timer. Timer just calls function that increments some variable.
I need to display the function's variable that is incremented, in TextBox.
In Form1.h I add code:
public: void Timer_Function(); //function activated by timer Tick
void Set_Text(String ^str); //function to set TextBox Text
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if (timer1->Enabled == false) timer1->Enabled = true;
else timer1->Enabled = false;
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
Timer_Function();
}
In My_app.cpp code like this:
#include "stdafx.h"
#include "Form1.h"
#include "resource.h"
using namespace test_staticfunc;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew Form1());
return 0;
}
void Form1::Timer_Function()
{
Timer_Func();
}
void Form1::Set_Text(String ^str)
{
textBox1->Text = str;
}
void Timer_Func()
{
static int I=0;
I++;
Form1::Set_Text(I.ToString());
}
Function Timer_Func() is specified in "resource.h" like this:
void Timer_Func();
I.e. I'm trying to display the current state of inner variable I of Timer_Func() by passing it to a Form1 public method Set_Text().
So. The error here is that Set_Text() is not a static method.
I tried to make it static, but got an error "ะก2227: The operand to the left of "->Text" is not a pointer to a class, structure, or union." How to get it right? In that case a static method is trying to implement a non-static method, right?
Or another way: to make an instance of Form1 - instead of
Application::Run(gcnew Form1());
insert code
Form1 ^My_form = gcnew Form1();
Application::Run(My_form);
And use Set_Text as non-static method for class instance My_form.
But My_form is available only in main()! I couldn't make My_form anywhere else. Is there way to make it global or something?
May be there are other ways to solve this problem?
Help, please! I've already searched several forums for answer but didn't find the answer. More precisely non of them suited.
P.S. Sorry for my bad english! ^_^

Is it possible to point a Typedef function pointer to a class member?

I am working with an executable that includes a DLL. For my testcase, I combined the code into a single executable. I am working with Visual Studio 2008 and Boost 1.43. I've tried researching this, but haven't found any clear answer. Thanks for the help.
In my main.h:
#include <string>
//These are normally defined in a seperate DLL
typedef std::string Typedef_func(const std::string & title);
void Register_My_Typedef(Typedef_func*);
//-------------------------------------------
class myClass
{
public:
std::string func_one(const std::string & title);
Typedef_func _test;
void run();
};
In my main.cpp:
#include "main.h"
#include <boost/bind.hpp>
std::string workingFunc(const std::string & title)
{
return "";
}
int main(int argc, char* argv[])
{
myclass* example;
example->run();
Register_My_Typedef(&workingFunc);//This works.
return 0;
}
void myClass::run()
{
//I want to point a Typedef_func* in a DLL to call myclass::func_one
Typedef_func* tf = boost::bind(&myClass::func_one, this, "test"); //This does not.
Register_My_Typedef(tf);
}
std::string myClass::funcOne(const std::string & title)
{
return "";
}
void Register_My_Typedef(Typedef_func* passedIn)
{
//Points the pointer in the DLL to passedIn
}
The DLL logic works fine when Register_My_Typedef is called on a function not in a class, but is it possible to call it from within a class? When I try to compile this code it returns:
When I try and compile in Windows XP with VS2008 I get:
Error C2440: 'initializing' : cannot convert from
'boost::_bi::bind_t' to 'Typedef_func (__cdecl *)' with
[
R=std::string,
F=boost::_mfi::mf1,
L=boost::_bi::list2,boost::_bi::value>
]
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called.
The Answer is Typedef itself is class member for a static member function & behave differently to non-static however works best reading rather class function of user defined class main.