|
I think that will do the trick. I don't know why I didn't think of it, because I already do some similar conversions just within my main app. All of a sudden I'm talking about crossing over between the main app and a DLL and I lost my head. Thanks for pointing out that article!
|
|
|
|
|
I create dynamic event delegates (see code at end of question), which call a common method. When an event is fired, my dynamic method is called and in turn calls the common method.
My problem is that when in the common method the 'this' pointer is meaningless (as if it thinks its a static), so I can't access members of my class. I have a temporary work around where I build in the members to the dynamic method and pass them to the common method! however it still breaks in the debugger and I have to <continue> but gives the desired result.
There is something not right with my dynamic method and if anyone can put me on the right track then that would be greatly appreciated.
Thanks
Peter.
void EventHandler::CreateDynamicEventHandler()
{
System::Type ^ eventClass = m_eventSource->GetTarget()->GetType();
// Get event info and the type of delegate.
array<system::type^>^ args;
EventInfo ^ eventInfo = eventClass->GetEvent(m_eventName);
System::Type ^ tDelegate = eventInfo->EventHandlerType;
MethodInfo^ invoke = tDelegate->GetMethod( "Invoke" );
// The generated delegate will call a standard method (CommonEventHandler)
// to pass the events data onto Jade. Get the method info for that method.
array<system::type^>^ commonArgs = {System::String::typeid, array<system::object^>::typeid};
MethodInfo ^ methodInfo = EventHandler::typeid->GetMethod("CommonEventHandler", commonArgs);
// Create dynamic method
array<system::type^> ^ paramTypes = GetDelegateParameterTypes(tDelegate);
DynamicMethod ^ dynamicMethod = gcnew DynamicMethod("Dyna",
nullptr,
paramTypes,
::EventHandler::typeid);
// Generate method body
ILGenerator ^ ilGen = dynamicMethod->GetILGenerator();
// Create a local variable 'args'
int paramCount = paramTypes->Length;
LocalBuilder ^ localObj = ilGen->DeclareLocal(array<system::object^>::typeid);
// create object array of proper length
ilGen->Emit(OpCodes::Ldc_I4, paramCount);
ilGen->Emit(OpCodes::Newarr, System::Object::typeid);
ilGen->Emit(OpCodes::Stloc_0);
// Now put all arguments in the object array
for (System::Int32 i= 0; i<paramcount; i++)
="" {
="" system::byte="" b="System::Convert::ToByte(i);
" ilgen-="">Emit(OpCodes::Ldloc_0); // Local variable - the array
ilGen->Emit(OpCodes::Ldc_I4, i); // Index into array
ilGen->Emit(OpCodes::Ldarg_S, b); // Value to save is in bth argument to this method
if (paramTypes[i]->IsValueType)
ilGen->Emit(OpCodes::Box, paramTypes[i]); // Box value types
ilGen->Emit(OpCodes::Stelem_Ref, i);
}
// Call common method after pushing parameters
ilGen->Emit(OpCodes::Ldarg_0); // 'this' ptr (required for instance calls)
ilGen->Emit(OpCodes::Ldstr, m_eventName); // Just to help with debugging!
ilGen->Emit(OpCodes::Ldloc_0);
ilGen->EmitCall(OpCodes::Call, methodInfo, nullptr);
ilGen->Emit(OpCodes::Ret);
// Create delegate (Also completes dynamic method build)
System::Delegate ^ del = dynamicMethod->CreateDelegate(tDelegate);
// Add delegate to event's invocation list
MethodInfo ^ addHandler = eventInfo->GetAddMethod();
array<system::object^> ^ addHandlerArgs = {del};
addHandler->Invoke(m_eventSource->GetTarget(), addHandlerArgs);
}
|
|
|
|
|
i, i am newbie in C++/CLI WinForms.
I am trying to send text the textBox1 of Form1 at label1 in Form2 but i can send text of textbox2 of Form2 at label2 in Form1.
The #include "Form1.h" in form2 give problems.
I think in use a ref class.
--[ controlWindows.h ]--
namespace ControlWindows
{
using namespace System;
using namespace System::Windows::Forms;
ref class CWindows
{
static Form ^WindowsX2;
//static Form2 ^WindowsX22; // ERROR
static bool X2Status = false;
public:
// FORM2
static void ShowFormX2();
static void CloseFormX2();
static void ChangeCaptionX2();
static void MngVentanaX2(Object ^Obj);
};
}
-- EOF controlWindows.h --
--[ ControlWindows.cpp ]--
#include "controlWindows.h"
using namespace ControlWindows;
#include "Form1.h"
#include "Form2.h"
using namespace formToform;
void CWindows::ShowFormX2()
{
if ( X2Status == false)
{
WindowsX2 = gcnew Form2;
WindowsX2->Show();
X2Status = true;
}
}
void CWindows::CloseFormX2()
{
// cierra la ventana
WindowsX2->Close();
Estado = false;
}
void CWindows::ChangeCaptionX2()
{
WindowsX2->Text = L"Nuevo Caption";
WindowsX2->Update();
}
void CWindows::MngVentanaX2(Object ^Obj)
{
// WindowsX2->textBox1->Text = L"ASDASD"; // i dont get it
WindowsX2->Text = Obj->ToString();
// WindowsX2->textBox1->Text = Obj->ToString();
}
-- EOF controlWindows.cpp --
The Form2 has textBox1 but "static Form ^WindowsX2;" i get only access to form virgin, i dont have access to real form2 object
any idea?
thank
|
|
|
|
|
To communicate in both directios, you should use delegates, or pass a reference to your form1 in to form2.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
???????
I do not understand what you say to me.
|
|
|
|
|
form2 needs a copy of form1, a pointer, to call it's methods. Or, you can use delegates. (clue: when people use a word you don't know, google will define it for you )
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Hi,
An application written in C#-VS2005 needs to use a class exported in a VC6Dll.
I have googled around a bit about Data Marshalling using P/Invoke. But all I could find is information on how to use the exported functions from a dll OR a struct from a dll.
I could not find any info on how to use the (exported,unmanaged) class of the dll in my C# App.
Any help or link woud be helpful.
Thanks and Regards,
Arti Gujare
|
|
|
|
|
Don't cross post especially don't cross post into a C++ forum with a C# question.
led mike
|
|
|
|
|
Apologies.
This Mixed Mode coding makes me confused sometimes. :
|
|
|
|
|
Hi all...
I am working with visual studio 2005.In my application ,i need to transfer dataset content to local database table.I am using MS Access .Please help me...
Thanks
vinod
|
|
|
|
|
Have you looked at the scores of Database articles here on CodeProject?
led mike
|
|
|
|
|
When using a Managed C++ Wrapper to expose methods to C# and VB.NET I get cannot create reference to this object when I transfer my compiled application to another machine. I am using C# and VB.NET by making a reference to it, and (Using or Import...), I am able to compile on a machine using VS-2005 CLI C++ and make a DLL. On the same machine I can reference this DLL, and utilize its exposed methods. This works all the time on the same machine that I compiled the CLI C++ DLL on, but when I copy the C# or VB source code and DLL to another machine, I have to re-run the Managed C++ Compile from source code in order to properly reference the DLL. What am I missing if I want to compile and distribute my VB or C# Application with this DLL? What is the compiler doing that I am not when I reference the DLL? Should I attempt to Sign and import it into the GAC or? What is the proper way of performing this?
|
|
|
|
|
Your app probably requires the redistributable for C++, and you also need to make sure you distribute the manifest file
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Given a Borland OWL application, I am curious if it is possible to display C# dialogs. I know OWL will allow calls into MFC DLLs. And I have already done work in an MFC application to display C# dialogs.
So it seems reasonable for OWL to call the MFC DLL which will call the C# DLL.
I am wondering if it will require the MFC DLL to be a regular DLL (derived from CWinApp), or if an extension DLL will also work.
At this time, I do not actually have a Borland OWL application, otherwise I would just try it out for myself. I am curious for a future possibility requiring this ability. I was thinking the OWL application's menu item can call into the MFC DLL, which will be a pass through to a C# DLL(s) containing various C# dialogs.
Thanks for any insight and help.
|
|
|
|
|
|
Oops. Sorry. In the past (meaning several months ago), I've seen this board used for MFC->Managed and MFC->.NET questions, whereas the C# board has been more focused directly on C# questions.
|
|
|
|
|
hi friends
i am new in c++
I have one file enc.cpp
i dnt know how to run it.
plz tell me how to run project in c++ .net
thanks in advance.
khan
|
|
|
|
|
imrankhanpathan wrote: I have one file enc.cpp
And what does it contain ? I mean function,classes etc.
Every C++ app need to have an entry point function. main for console app. for instace.
Does your file contains such function ?
BTW, you need to ask this question in VC++ forum.
|
|
|
|
|
hi prasad
thanks ur reply
actually i download one project for encryption and has a many functions,one is entry point in enc.cpp.
so plz tell me now how to run it.
thanks again
khan
|
|
|
|
|
You need to include it in a project. And, as has been said, you're in the wrong forum.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
In C/C++ what r the differences between header file(.h)
& library files(.lib). Which of them contains function
Declaration and which contains Definition?
|
|
|
|
|
A header file contains source code. A lib file contains object code (already compiled).
By convention, header files contain declarations, inline definitions, and any other source code
that needs to be shared by multiple source files.
|
|
|
|
|
Hi all, i was curious to know how would i program a function to create a file into any directory. For example. Most windows dirs begin with c:\
But what if they have a custom dir? Like E:\ or H:\ ?
I would just like to know if i can make a function that will add a file in the folder no matter what the directory begins with. Thanx in advance!
|
|
|
|
|
You want to do this in managed C++, or are you in the wrong forum ?
You'd generally let the user choose the directory they wanted to create into, or you'd use the path to your executable as a starting point, if you wanted to create it locally.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Hi all,
well allthough I'm new to C++ I've done my share of development in more user friendly environments, mainly C#.
Anyhow I want to learn C++ as mainly personal interest as my professional life will be focused primarly in .NET development whenever it is needed.
To learn the basics of the language I'm starting with a real easy Math class that will initially implement a Matrix (no templates). Nothing fancy but an easy place to start to at least start learning Object Oriented programming in C++ like any other.
Well the thing is that I'm completely stumped with an error I'm getting. I've created a simple Win32 console app that links my FastMath.dll and uses the FMatrix class defined inside. Test app code is as simple as it can get, it only instantiates one FMatrix and then deletes it, and something that simple is giving me a runtime error. Here's the code in the tesp app:
#include "stdafx.h"
#include "FMatrix.h"
#include <iostream>
using namespace FastMath;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
FMatrix *a=new FMatrix(5);
cout << "A is a [" << a->GetRowCount() << "x" << a->GetColumnCount() << "] matrix." << endl;
delete a;
return 0;
}
Ok the error I'm getting is when executing "delete a":
Windows has triggered a breakpoint in Tester.exe.
This may be due to a corruption of the heap, and indicates a bug in Tester.exe or any of the DLLs it has loaded.
The output window may have more diagnostic information
If I try to continue I get the same error once and then succesive Assertion Failed error messages: "Expression: _CrtIsValidHeapPointer(pUserData)" and if I ignore that I get a HEAP CORRUPTION DETECTED error message "CRT detected that the application wrote to memory after end of heap buffer."
I really do not understand what I'm doing wrong in my FMatrix file. I'm including header and source files below:
FMatrix.h
#ifdef FASTMATH_EXPORTS
#define FASTMATH_API __declspec(dllexport)
#else
#define FASTMATH_API __declspec(dllimport)
#endif
#pragma once
#include "StdAfx.h"
namespace FastMath
{
class FASTMATH_API FMatrix
{
private:
int rw,cl;
double *val;
FMatrix(void);
FMatrix(unsigned int,bool);
FMatrix(unsigned int,unsigned int,bool);
void initMatrix(unsigned int,unsigned int,bool);
void destroy(void);
public:
FMatrix(const FMatrix&);
FMatrix(unsigned int);
FMatrix(unsigned int,unsigned int);
FMatrix(unsigned int,unsigned int,double**);
~FMatrix(void);
static FMatrix* CreateIdentity(unsigned int);
static FMatrix* CreateFull(unsigned int,unsigned int,double);
static FMatrix* CreateRandomIntMatrix(unsigned int,unsigned int,int,int);
static FMatrix* CreateRandomDblMatrix(unsigned int,unsigned int,double,double);
double GetItem(unsigned int,unsigned int) const;
void SetItem(unsigned int,unsigned int,double);
int GetRowCount() const;
int GetColumnCount() const;
static bool AreSameSize(const FMatrix&,const FMatrix&);
FMatrix& operator =(const FMatrix&);
FMatrix& operator -() const;
FMatrix& operator +(const FMatrix&) const;
FMatrix& operator -(const FMatrix&) const;
FMatrix& operator *(double) const;
FMatrix& operator *(const FMatrix&) const;
FMatrix& operator !() const;
bool operator ==(const FMatrix&) const;
bool operator !=(const FMatrix&) const;
};
}
FMatrix.cpp
#include "StdAfx.h"
#include "FMatrix.h"
#include "time.h"
#include <cstdlib>
#include <iostream>
using namespace FastMath;
using namespace std;
void FMatrix::initMatrix(unsigned int r,unsigned int c, bool initValues)
{
rw=r;
cl=c;
val=new double[rw*cl];
#ifdef _DEBUG
cout <<"++ Matrix created." << endl;
#endif
if (initValues)
{
for (int i=0;i<rw;i++)
{
for (int j=0;j<cl;j++)
*(val+i*cl+j)=0;
}
}
}
FMatrix::FMatrix()
{
}
FMatrix::FMatrix(unsigned int dim,bool initValues)
{
initMatrix(dim,dim,initValues);
}
FMatrix::FMatrix(unsigned int rows,unsigned int columns,bool initValues)
{
initMatrix(rows,columns,initValues);
}
FMatrix::FMatrix(unsigned int dim)
{
initMatrix(dim,dim,true);
}
FMatrix::FMatrix(unsigned int rows, unsigned int columns)
{
initMatrix(rows,columns,true);
}
FMatrix::FMatrix(const FMatrix &m)
{
initMatrix(m.rw,m.cl,false);
for (int i=0;i<rw;i++)
{
for (int j=0;j<cl;j++)
*(val+i*cl+j)=*(m.val+i*cl+j);
}
}
FMatrix::~FMatrix()
{
destroy();
}
void FMatrix::destroy()
{
delete[] val;
#ifdef _DEBUG
cout << "-- Matrix destroyed." << endl;
#endif
}
FMatrix* FMatrix::CreateIdentity(unsigned int dim)
{
FMatrix *t=new FMatrix(dim,false);
double* pTval=t->val;
int c=t->cl;
for (register int i=0;i<t->rw;i++)
{
for (register int j=0;j<c;j++)
{
if (j==i)
*(pTval+i*c+j)=1;
else
*(pTval+i*c+j)=0;
}
}
pTval=NULL;
return t;
}
FMatrix* FMatrix::CreateFull(unsigned int rows, unsigned int columns, double value)
{
FMatrix *t=new FMatrix(rows,columns,false);
double* pTval=t->val;
int c=t->cl;
for (register int i=0;i<t->rw;i++)
{
for (register int j=0;j<c;j++)
{
*(pTval+i*c+j)=value;
}
}
pTval=NULL;
return t;
}
FMatrix* FMatrix::CreateRandomIntMatrix(unsigned int rows, unsigned int columns, int lowest, int highest)
{
FMatrix *t=new FMatrix(rows,columns,false);
double* pTval=t->val;
int c=t->cl;
SYSTEMTIME lp;
GetSystemTime(&lp);
srand(rand()*(unsigned int)lp.wMilliseconds);
for (register int i=0;i<t->rw;i++)
{
for (register int j=0;j<c;j++)
{
*(pTval+i*c+j)=int(lowest+double(rand())/RAND_MAX*(highest-lowest)+.5);
}
}
pTval=NULL;
return t;
}
FMatrix* FMatrix::CreateRandomDblMatrix(unsigned int rows, unsigned int columns, double lowest, double highest)
{
FMatrix *t=new FMatrix(rows,columns,false);
double* pTval=t->val;
int c=t->cl;
SYSTEMTIME lp;
GetSystemTime(&lp);
srand((unsigned int)lp.wMilliseconds);
for (register int i=0;i<t->rw;i++)
{
for (register int j=0;j<c;j++)
{
*(pTval+i*c+j)=lowest+double(rand())/RAND_MAX*(highest-lowest);
}
}
pTval=NULL;
return t;
}
FMatrix& FMatrix::operator =(const FMatrix &m)
{
if (this!=&m)
{
double* pMval=m.val;
rw=m.rw;
cl=m.cl;
destroy();
initMatrix(rw,cl,false);
for (register int i=0;i<rw;i++)
{
for (register int j=0;j<cl;j++)
{
*(val+i*cl+j)=*(pMval+i*cl+j);
}
}
pMval=NULL;
}
return *this;
}
FMatrix& FMatrix::operator -() const
{
FMatrix *t=new FMatrix(rw,cl,false);
double* pTval=t->val;
for (register int i=0;i<rw;i++)
{
for (register int j=0;j<cl;j++)
{
*(pTval+i*cl+j)=-*(val+i*cl+j);
}
}
pTval=NULL;
return *t;
}
FMatrix& FMatrix::operator +(const FMatrix &m) const
{
if (!AreSameSize(*this,m))
{
throw exception("Matrix can not be added. Dimensions do not match.");
}
FMatrix *t=new FMatrix(rw,cl,false);
double* pTval=t->val;
double* pMval=m.val;
for (register int i=0;i<rw;i++)
{
for (register int j=0;j<cl;j++)
{
*(pTval+i*cl+j)=*(val+i*cl+j)+*(pMval+i*cl+j);
}
}
pTval=NULL;
pMval=NULL;
return *t;
}
FMatrix& FMatrix::operator -(const FMatrix &m) const
{
if (!AreSameSize(*this,m))
throw invalid_argument("Matrix can not be added. Dimensions do not match.");
FMatrix *t=new FMatrix(rw,cl,false);
double* pTval=t->val;
double* pMval=m.val;
for (register int i=0;i<rw;i++)
{
for (register int j=0;j<cl;j++)
{
*(pTval+i*cl+j)=*(val+i*cl+j)-*(pMval+i*cl+j);
}
}
pTval=NULL;
pMval=NULL;
return *t;
}
FMatrix& FMatrix::operator *(double d) const
{
FMatrix *t=new FMatrix(rw,cl,false);
double* pTval=t->val;
for (register int i=0;i<rw;i++)
{
for (register int j=0;j<cl;j++)
{
*(pTval+i*cl+j)=*(val+i*cl+j)*d;
}
}
pTval=NULL;
return *t;
}
FMatrix& FMatrix::operator !() const
{
FMatrix *t=new FMatrix(cl,rw,false);
double* pTval=t->val;
for (register int i=0;i<rw;i++)
{
for (register int j=0;j<cl;j++)
{
*(pTval+j*rw+i)=*(val+i*cl+j);
}
}
pTval=NULL;
return *t;
}
bool FMatrix::operator ==(const FMatrix &m) const
{
if (!AreSameSize(*this,m))
return false;
double *pMval=m.val;
for (register int i=0;i<rw;i++)
{
for (register int j=0;j<cl;j++)
{
if (*(pMval+i*cl+j)!=*(val+i*cl+j))
return false;
}
}
pMval=NULL;
return true;
}
bool FMatrix::operator !=(const FMatrix &m) const
{
return !(*this==m);
}
double FMatrix::GetItem(unsigned int r, unsigned int c) const
{
return *(val+r*cl+c);
}
void FMatrix::SetItem(unsigned int r, unsigned int c, double value)
{
*(val+r*cl+c)=value;
}
int FMatrix::GetRowCount() const
{
return rw;
}
int FMatrix::GetColumnCount() const
{
return cl;
}
bool FMatrix::AreSameSize(const FMatrix &m1, const FMatrix &m2)
{
return (m1.rw==m2.rw)&&(m1.cl==m2.cl);
}
I'm really confused because I dont know what I'm doing wrong. The thing is if I use in the App test the static constructors (CreateRandomIntMatrix for example) and I dont have to manually delete them from the app test everything works greate and the destructor of my two matrixes is called all by itself. But when I explecitly instantiate with the new operator in my app test and thus have to delete them myself in that same app I get the errors described above.
Any help plz?
P.D. Sorry about the long post.
|
|
|
|
|