|
I'm using System.Net.NetworkInformation.NetworkInterface to get the information about network connections to my PC. But this class seems to be 'Read only', i.e., I can view the information about network connection. For example System.Net.NetworkInformation.NetworkInterface.OperationalStatus tells us whether network connection is Up or Down. Is there any away that I can change that status programatically? i.e, can we enable/disable the network connection programatically?
Thanks
Ram
|
|
|
|
|
Hi,
I've the following code that runs in a timer function every second. OnTimedEvent() is a timer delegate that is invoked by Windows timer every second. It uses MFC/Windows SDK. It sets the processor priority for the thread that is running this code.
void CPci1553::OnTimedEvent( Object^ source, ElapsedEventArgs^ e )
{
//set the timer thread affinity to second process and set it to higher priority
SYSTEM_INFO info;
//get the configuration information of computer
GetSystemInfo(&info);
//if the system has more than one processor, use the last processor for TOD thread
//set the thread affinity to second processor
if(info.dwNumberOfProcessors > 1)
SetThreadIdealProcessor(GetCurrentThread(),info.dwNumberOfProcessors-1);
}
I need to upgrage this code for .NET using C++/CLI. I tried to use .NET functions like "System::Environment::ProcessorCount" to processor count in on the system, "System.Diagnostics.ProcessThread.IdealProcessor" to set the processory priority for the thread, "System.Threading.Thread.CurrentThread" to get the current running thread for this code. But the problem is "System.Diagnostics.ProcessThread" is unmanaged thread and "System.Threading.Thread" is managed thread. I don't know how to mix these two. Pls. help me with this code.
Thanks
Ram
|
|
|
|
|
There isn't a one-to-one relationship between System.Threading.Threads
and System.Diagnostics.ProcessThreads.
A System.Diagnostics.ProcessThread is an actual OS thread.
A System.Threading.Thread is a logical thread. It's possible for many
System.Threading.Threads to be using the same OS thread.
You can manage affinity at either level, but doing it at the
System.Threading.Thread level doesn't mean it will have any effect whatsoever
on an OS thread, like your Win32 API code does.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
OK. If I want to go with OS thread using System.Diagnostics.ProcessThread class, I can use System::Diagnostics::ProcessThread::IdealProcessor to set the processory affinity. But, how do I access the current running thread like we get with GetCurrentThread()in WIN32?
|
|
|
|
|
Hi I'm creating an application in Windows forms. I wondering if any knows how a System::Diagnostics::Process::Start("C:\\path\\my.exe"); which opens an application in a window to place that window in my form?
|
|
|
|
|
This[^] is in C#, but it should help.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Hi, this is probably an absurd question, but I'm really new to working in C++/CLI, so please bare with me. I have a solution that compiles into an application (.exe). I was wondering if it was at all possible to convert the project into Custom Windows Form control, so that I can place it in a Wondows form. The application is Scite, btw. Or if anyone knows how to get a Scintilla based text editor to be a Forms Control, that would also help.
Thank you!
|
|
|
|
|
Check this[^] out.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Hi, i'm using windows forms and I was wondering how to add to a listBox from a comboBox. Also how to remove an item from the listBox.
<br />
private: System::Void btnConfirmCommand_Click(System::Object^ sender, System::EventArgs^ e) {<br />
<br />
this->cmbCommands->Select();<br />
this->lstCommands->Items->Add(cmbCommands);<br />
<br />
}<br />
<br />
<br />
private: System::Void btnRemoveCommand_Click(System::Object^ sender, System::EventArgs^ e) {<br />
<br />
this->lstCommands->Select();<br />
this->lstCommands->Items->Remove(lstCommands);<br />
<br />
}<br />
modified on Thursday, January 31, 2008 10:27:48 AM
|
|
|
|
|
newkid wrote: this->lstCommands->Items->Add(cmbCommands);
You need to pass in the actual string you want to add not the ComboBox control.
led mike
|
|
|
|
|
so i create a string say x, how do you set an item in the comboBox to x?
<br />
string x;<br />
<br />
this->cmbCommands->Select();<br />
cmbCommands->SelectedItem;<br />
<br />
this->lstCommands->Items->Add("x");<br />
<br />
|
|
|
|
|
Are you in school or leaning by working from a book or what? If you are using a book to start out I think you might need a different book, or you may have skipped some stuff or something. This is very very beginner level stuff and you are already on to list boxes and combo boxes without knowing it. That's probably not a good idea.
led mike
|
|
|
|
|
This should solve your problem.
<br />
<br />
if(cmbCommands->Text->Length > 0)<br />
{<br />
lstCommands->Items->Add(cmbCommands->Text);<br />
<br />
}<br />
<br />
if(lstCommands->SelectedIndex !=-1)<br />
{<br />
lstCommands->Items->RemoveAt(lstCommands->SelectedIndex);<br />
}<br />
|
|
|
|
|
rover_boy wrote: This should solve your problem.
Really? It will solve this?
newkid wrote: so i create a string say x, how do you set an item in the comboBox to x?
string x;
this->lstCommands->Items->Add("x");
I think not.
led mike
|
|
|
|
|
Thanks thats sovled the problem
|
|
|
|
|
Hello everyone,
I have a lot of MFC control classes in C++,and I want to wrap them with C++/CLI and resue them in C# Application. but I do not know how to wrap them.
Can anyone give an hand? Thanks a lot.
|
|
|
|
|
Birch Howe wrote: Can anyone give an hand?
Look at the Articles here on CodeProject on the subject C++/CLI (look in the menu bar on the side of the page under "Chapters"). There is nothing specific about wrapping MFC other than any compiler/linker settings one might need and of course the dependency issues on deployment. Otherwise you are just wrapping a Native C++ class in a managed C++/CLI class.
led mike
|
|
|
|
|
Hello,
I think Nishant Sivakumar's book "C++\CLI in action" talks about this specific topic in "6.6 Using an MFC control in a Windows Forms form" titled chapter.
I've not tried yet... but i hope this helps.
|
|
|
|
|
I have callback[delegate] in managed code being called by native code. It works fine the first time but on the next call the native code blows up. Bear with me the structure is a little twisted, but I'll try my best to explain.
Here's the general structure:
Client
|
--->ServiceInterface
|
--->NativeService
The main question is how should I be passing the callback to the native code from the ServiceInterface initialization?.
Should the delegate be pinned before passing it to the native code?
The following is not exact code but a snapshot of how the code is interacting.
// Managed code
#include "ServiceInterface.h"
ref class Client
{
public:
ServiceInterface ^mngService;
Client()
{
mngService = gcnew ServiceInterface(this);
}
void initialize()
{...}
void Task1()
{
mngService->registerCallback(Task1_Callback);
}
void Task2()
{
mngService->registerCallback(Task2_Callback);
}
static int Task1_Callback(void *pdata, MessageType event)
{
Console::WriteLine(L"Task 1 completed...");
// cast and remove listener for task 1
return 1;
}
static int Task2_Callback(void *pdata, MessageType event)
{
Console::WriteLine(L"Task 2 completed...");
// cast and remove listener for task 2
return 2;
}
};
// ServiceInterface.h
#include <nativecode.h>
public delegate int Callbackfunction(void *pdata, MessageType event);
ref class ServiceInterface
{
private:
static list<Callbackfunction> callbacklist;
NativeService *service;
static Callbackfunction ^MessageProcessor;
static GCHandle ghMasterCallback;
static IntPtr ipMasterCallback;
ServiceInterface(Object ^client)
{
service = new NativeService();
//// Thought this was the way to pass function pointer to native code,
//// but crashed on the first callback.
//MessageProcessor= gcnew Callbackfunction(&Service::MessageEngine);
//ghMasterCallback = GCHandle::Alloc(MessageProcessor);
//ipMasterCallback = Marshal::GetFunctionPointerForDelegate(MessageProcessor);
//service->RegisterEventProcessor((Callbackfunction)ipMasterCallback.ToPointer(), client(Void * Value));
// Instead this method seem to work, at least for the first callback but dies on the second callback[Task 2]
service->RegisterEventProcessor((Callbackfunction)&MessageEngine, client(Void * Value));
callbacklist = new list<Callbackfunction>;
}
void registerCallback(Callbackfunction ^callback)
{
callbacklist->push_back(callback);
}
void removeCallback(Callbackfunction ^callback)
{ // removes the given callback function}
// Registered as EventProcessor with NativeCode
// Calls delegates in managed code
int MessageEngine(void *pdata, MessageType event)
{
list <Callbackfunction>::iterator Iter;
for ( Iter = callbacklist.begin( ); Iter != callbacklist.end( ); Iter++ )
static_cast<Callbackfunction>(*Iter)(pdata,event);
}
};
// UNManaged code
// nativecode.h
enum MessageType
{
EVENT_START,...
};
typedef int (* Callbackfunction)(void *pdata, MessageType event);
class NativeService
{
private:
void *clientContext;
public:
NativeService(){...}
Initialize(){...}
// Callback function to be defined in managed interface
Callbackfunction EventProcessor;
DoWorkFunction
{
:
:
// Message generated from service
MessageType newMessage;
DispatchCallback(EventProcessor(clientContext, newMessage));
}
RegisterEventProcessor(Callbackfunction processor, void * client)
{
EventProcessor = processor;
clientContext = client;
}
};
|
|
|
|
|
Hi,
I did not study your code in detail, but I remember from earlier work the most probable
cause for a "works one time then bombs" situation is a mismatch in the calling conventions
(stdcall, CDecl, that kind of stuff). When calling from managed to unmanaged, you
can choose on both sides and of course you must choose the same on both sides; when
calling from unmanaged to managed, the managed side is fixed, and you must add the
right keywords to make the unmanaged side match the managed side.
Don't trust the defaults, look it up and use the correct calling convention explicitly.
[ADDED]
Another thing to keep in mind is the different sizes of some primitive data types;
among the most confusing are long (32bit unmanaged, 64bit managed) and char (8bit unmanaged,
16bit managed).
Also, if Win64 is relevant, make sure to pass all handles (as well as lParam and wParam)
as IntPtr, which takes either 32-bit or 64-bit depending on the platform.
[/ADDED]
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- 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 PRE tags to preserve formatting when showing multi-line code snippets.
modified on Wednesday, January 30, 2008 9:58:01 PM
|
|
|
|
|
Thanks for the feedback Luc. I'll double check those types, even though I doubt that's the root of the issue.
Thanks,
Jason
|
|
|
|
|
I am working on a winamp plugin in managed c++. i have another dll referenced in my project that i need to use (in C#). Everything compiles fine, however when the dll is loaded into winamp it throws a FileNotFoundException stating it can't load the C# dll. the dll is in the same directory as the c++/cli. what do i need to do to get the assembly detected?
thanks in advance,
Don't be overcome by evil, but overcome evil with good
|
|
|
|
|
teejayem wrote: FileNotFoundException
Post the exception message
led mike
|
|
|
|
|
Could not load file or assembly 'Facebook, Version=1.6.2951.20499, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
Don't be overcome by evil, but overcome evil with good
|
|
|
|
|
teejayem wrote: it can't load the C# dll. the dll is in the same directory as the c++/cli. what do i need to do to get the assembly detected?
teejayem wrote: assembly 'Facebook, Version=1.6.2951.20499,
So the DLL is "Facebook.dll"? Does of the DLL match the one in the exception message?
teejayem wrote: I am working on a winamp plugin in managed c++.
Is the winamp interface to your DLL a native C++ interface? If so then there is probably some way (configuration data) to tell winamp where your DLL is right? So when the .NET runtime is loaded into the Winamp process, the .NET runtime doesn't know anything about the Winamp configuration data that points to the folder with those DLL's. The .NET runtime is using the current working folder of the Winamp process to look for that assembly, plus of course however else it looks for assemblies, but it certainly won't look in a folder that is only known to the Winamp native code.
led mike
|
|
|
|