|
I think you must either make the windows forms STA or run your dll code in a separate STA thread.
var thread = new Thread(() => { new SaveFileDialog().ShowDialog(); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
Eslam Afifi
|
|
|
|
|
I think that should be the way, because the *.exe file that use my dll it is not a managed project, so it hasn´t the property STA.
Thank you,
|
|
|
|
|
You're welcome.
Eslam Afifi
|
|
|
|
|
hi. i have a class like this :
public ref class MyClass : public IDisposable
{
.
.
.
public: MyClass();
private: void Wait();
.
.
.
}
and the cpp file:
MyClass::MyClass()
{
Thread^ t = gcnew Thread(gcnew ThreadStart(&MyClass::Wait));
t->Name = "Event Thread";
t->Start();
}
in compile i got this error :
"error C3350: 'System::Threading::ThreadStart' : a delegate constructor expects 2 argument(s)"
I've looked in msdn and saw a example that exactly used this way for threading but there was no error in compilation.
thank you for help.
sometimes 0 can be 1
|
|
|
|
|
erfi wrote: I've looked in msdn and saw a example that exactly used this way for threading
So why didn't you post a link to the documentation you claim to have seen?
Try looking at the example code in this documentation[^]
|
|
|
|
|
erfi wrote: I've looked in msdn and saw a example that exactly used this way
Exactly?
I have a feeling the Wait() function was static in the example.
Using a non-static function as you've shown won't work because a MyClass
object (instance) is required to make the call to Wait(). You'd have to pass
an instance to the delegate constructor.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi;
I'm finally digging into Reflection and thought, as an exercise, to create an instance of MessageBox and then to modify some of it's (private) properties, such as the font.
Using GetFields() to extract the FieldInfo, all I can get a few items out of it (static values which are, effectively, the return values). As for get Constructor: I get nothing. Creating an instance wants args (for the constructor) which I can't seem to get.
Testing on a very simple home-made method, I can read/write to public/protected/private fields, and the above listing methods (for fields, constructors) works fine.
Any comments and/or steering suggestions for my quest? (code snippets below)
Balboos
Reflection::Assembly ^a = Reflection::Assembly::GetAssembly(MessageBox::typeid);
array<type^>^ typeArray = a->GetTypes();
Type ^TypeToReflect = typeArray[1213];
Object ^n = TypeToReflect->InvokeMember("IDOK", BindingFlags::GetField | BindingFlags::Public | BindingFlags::NonPublic | BindingFlags::Instance | BindingFlags::Static, nullptr, TypeToReflect, nullptr );
array<system::reflection::fieldinfo xmlns:system="#unknown"> ^fields = TypeToReflect->GetFields(
BindingFlags::Public | BindingFlags::NonPublic | BindingFlags::Instance | BindingFlags::Static);
array<system::reflection::constructorinfo> ^Members = TypeToReflect->GetConstructors();
</system::reflection::constructorinfo></system::reflection::fieldinfo>
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"How do you find out if you're unwanted if everyone you try to ask tells you to stop bothering them and just go away?" - Balboos HaGadol
"It's a sad state of affairs, indeed, when you start reading my tag lines for some sort of enlightenment. Sadder still, if that's where you need to find it." - Balboos HaGadol
|
|
|
|
|
MessageBox is a static class, there isn't any constructor, there is just one useful method and all it does is wrap the native MessageBox function. It probably is the worst class for getting acquainted with reflection!
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Tuesday, March 31, 2009 12:01 PM
|
|
|
|
|
Thanks - I can't stop spinning those wheels (an exhausting job, this running in place).
Ok - So it's a bad choice.
Still, why can't I get to any of it's internal info (particularly members), aside from the enum of return values, via reflection? I suppose when I start to understand what I'm doing, that will become clear. The link to the native version brings to mind MessageBoxA (of what seems so long ago).
I'll certainly choose another target that can be interesting. The static classes, like MessageBox, seemed to hold so much promise for tweaking fun.
Once again, thanks.
Balboos
|
|
|
|
|
I have a Windows service written in C#, that needs to load a C++ SDK. I have written CLI classes, that acts as a bridge. The problem is that my CLI code fails at the point where it makes call to the C++ SDK. I am not getting any error message also. It just hangs. If I try the same from Win Forms application, I did not see any problem. For me it looks like there is issue only when using Windows service. Is it possible to consume CLI\C++ dll's inside windows serive?
Please clarify me on this.. Any link related to the problem will be much helpful.
|
|
|
|
|
BicycleTheif wrote: Is it possible to consume CLI\C++ dll's inside windows serive?
Yes.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi Everyone:
Recently while I've been trying to watch the TV, there are pop-up ads that cover up the bottom quarter of the screen that occur at regular intervals. This is especially annoying when the program contains subtitles. This occurs on every commercial TV network.
Has anyone thought about making a device we can attach to our cable boxes that can filter out the pop ups? Since there is one for the internet I thought there might be one for cable TV.
I wouldn't know where to begin.
Thanks,
Quecumber256
|
|
|
|
|
I'm working on a mixed-mode project, and I came across an impossible situation today. One piece of my code managed code was not working, and I narrowed it down to the fact that it's evaluating a FALSE value as TRUE!
Don't believe me? Take a look at the screen shot of my debugger.
Line 1458 contains the IF clause that is mis-evaluating. You can see the step-trace arrow has navigated into line 1459. The very explicit comparison "...bCamFollowsTrain == true" evaluates to true even though the debugger watch window very clearly shows that bCamFollowsTrain == false. So therefore false == true!
If I continue step-tracing into the CameraMove() call at 1460, that method also contains a check on the same variable and also evaluates to "true" even though the watch window continues to show that its value is "false".
All I can think here is memory corruption. Does anyone else have any ideas? Thanks.
modified on Saturday, March 28, 2009 6:21 PM
|
|
|
|
|
I found the problem...
First, the dialog where the user can set the value of bCamFollowsTrain had backwards logic. When the "yes" radio button was selected, bCamFollowsTrain got assigned a value of FALSE, and vice versa. That's my bad. Once I fixed that, my code functioned as expected.
So then the question became "Why is the Watch window showing me the wrong value?" I inserted a line to explicitly update the value of bCamFollowsTrain just before the IF statement. In step tracing, I noticed then that the value in the Watch window shows correctly, regardless of whether I assign true or false.
The user updates this value via a modeless dialog box. Since the dialog is modeless, the update is performed by delegate callback in the RadioButton_SelectedIndexChanged event. When I removed the explicit assignment code and went back to using this dialog, the Watch window began showing the wrong value again. In fact, I was able to duplicate the situation in reverse, making the debugger show bCamFollowsTrain == TRUE when I had set it to FALSE (and when step-tracing took the FALSE path).
Therefore, my conclusion is that it seems the Watch window has some issues with picking up on variables changed via delegate callbacks. As of right now, my code is working completely as expected while my debugger is still showing incorrect values.
*sigh*
|
|
|
|
|
Hi
I currently working on building a VC++.NET wrapper for a C++ DLL. Everything compiles fine, but for some reason any methods that require pointers to be passed do not appear to be public in the wrapper class. As an example:
The prototype for a function in the C++ DLL might be:
const char* error_msg( );
so I create a corresponding function in the VC++.NET wrapper class as:
String^ o10xxBase::flash_m::error_msg()
{
return(gcnew System::String (m_flash->error_msg()));
}
This seems to work fine. Another example that requires input parameters... the prototype for the C++ function might be:
void WriteIoRegisterDW( unsigned int addr, unsigned int data );
so I create a corresponding function in the VC++.NET wrapper class as:
void o10xxBase::o10xxIo_m::WriteIoRegisterDW( unsigned int addr, unsigned int data )
{
(m_o10xxIo->WriteIoRegisterDW( addr, data ));
}
Again this works fine it seems. However if the C++ prototype requires a pointer input things don't work so well. For example, prototype for the C++ function might be:
void read_fifo( short *p_Buffer, int count );
so I create a corresponding function in the VC++.NET wrapper class as:
void o10xxBase::adi_9446_a2d_m::read_fifo( short * p_Buffer, int count )
{
(m_adi_9446_a2d->read_fifo( p_Buffer, count ));
}
This compiles with no errors, and yet the function does not appear in the wrapper class even though it is listed as public. Seems to be some issue I am overlooking with the movement of pointers from managed to unmanaged code.
Any thoughts most welcome.
Kind regards, Kurt
|
|
|
|
|
Kurt Richardson wrote: I am overlooking with the movement of pointers from managed to unmanaged code.
Maybe because there's really not any pointers in managed code
Your wrapper functions should use managed types for the arguments, not native C++ types,
otherwise other .NET languages won't be able to use your interface, which pretty much makes
a managed interface useless.
Maybe you could pass an array<Int16>^ instead, and marshal it to/from your native code if necessary...
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks Mark
I realize there are no pointers (in the C++ sense) in managed code (although getting the address of any variable is easily done), but I can't figure out what the marshal statement needs to be or even if marhsaling is needed... maybe I just pass the parameter ByVal and then just insert '&' into the C++ DLL call...
Bye for now, Kurt
|
|
|
|
|
It's ok for generic wrapper class (c++ -> cli) like this:
class Unmanaged {
...
};
namespace Wrapperspace
{
public ref class Managed {
protected:
Unmanaged *p;
public:
...
...
};
}
I could call the functions of Managed class from C# application
by just adding a reference to Wrapperspace.
But this technique didn't work for CDialog class.
class CTestDialog : CDialog {
...
};
namespace WrapperspaceDialog
{
publci ref class TestDialog {
protected:
CTestDialog *p;
public:
...
void ShowTest();
};
}
namespace WrapperspaceDialog
{
...
void TestDialog::ShowTest()
{
p = new CTestDialog();
p->Create(IDD_TESTDIALOG);
p->ShowWindow(SW_SHOW);
}
...
}
The compile is fine but when I call the ShowTest() in my application
the debug assertion failed in afxwin1.inl line 24.
Any help to write a correct wrapper class for dialog class?
Any hint will be great. Thank you in advance.
|
|
|
|
|
Oldboy2 wrote: when I call the ShowTest() in my application
the debug assertion failed in afxwin1.inl line 24.
I don't have any idea what's at that line in your framework code, but...
CDialog is an MFC class. It's also a CWnd-derived MFC class.
That means MFC needs to be properly initialized and running its message loop
for any of this to work. Have you handled those correctly?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Holy Mackerel, Mark is back!
|
|
|
|
|
<=><
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thank you for your advice. Adding proper mfc initializing and complex project configuration, I could open the mfc dialog finally.
|
|
|
|
|
Hi,
I have a MFC application. How can i give a short cut key to a button in toolbar? Is it define in ".rc" file?
Thanks & Regards
Shaheen
|
|
|
|
|
This[^] is the best place for MFC related questions.
|
|
|
|
|
hi !! can some one help me too Write a program like patchwork that takes two characters c and d, and a positive integer n and displays the following object made of the characters c and d over n
lines and n columns, as shown below.
(in for loop if that posible)
Sample 1:
Enter two characters and an integer: $ * 3
$*$
*$*
$*$
Sample 2:
Enter two characters and an integer: / \ 6
/\/\/\
\/\/\/
/\/\/\
\/\/\/
/\/\/\
\/\/\/
=======================================
i have tried something like this
#include
using namespace std;
int main() {
int n;
char c1, c2;
cout << "Enter two characters and an integer: " << flush;
cin >> c1 >> c2 >> n;
for (int i = 2; i < n; i++)
{
cout >> c1 >> c2;
}
return 0;
}
========================================
and
#include
using namespace std;
int main() {
int n;
char c1, c2;
cout << "Enter two characters and an integer: " << flush;
cin >> c1 >> c2 >> n;
for (int i = 2; i < n; i++)
{
cout << c1 << c2 << endl;
for (int j = 2; j <= n-1 ; j++)
cout << c2 << c1 ;
cout << endl;
}
return 0;
}
=============================<pre>
but it turn ot to be different to what i want and i dont know what to do next .
|
|
|
|