|
Hello,
I am trying to get current login user name using my own windows service program using VC++.Net 2005, windows XP. The code used to develop windows service contains only unmanaged one. But in order to retrieve the current login name.. i am using the help of System::Management class, for which i included necessary namespaces in its .cpp file.
actually i am trying interpret the following statement of c# into c++:
System::Management::Collections mc;
foreach(System::Management::Object mo in mc){..do this..}
my approach was somewhat like this in the following way:
CString CUSBActivity::Get_User_Name()
{
CString Username=_T("");
System::Management::ManagementObjectSearcher^ q = nullptr;
System::Management::ManagementObjectCollection^ mc = nullptr;
System::Management::ManagementObject^ mo = nullptr;
System::Collections::IEnumerator^ enu = nullptr;
q = gcnew System::Management::ManagementObjectSearcher("Select * from Win32_Process");
mc = q->Get();
enu = mc->GetEnumerator();
mo = (System::Management::ManagementObject^)enu->Current;
while(enu->MoveNext())
{
DebugBreak();
mo = (System::Management::ManagementObject ^)enu->GetType();
Username = mo["username"]->ToString();
mc->GetEnumerator()->MoveNext();
}
return Username;
}
But when i run this, i am getting the run time exception and the Username is null.
Could anybody... assist me in fixing this thank u.
Anee
|
|
|
|
|
In your while loop you do this:
mo = (System::Management::ManagementObject ^)enu->GetType();
That doesn't look like a valid cast to me.
Did you mean
mo = (System::Management::ManagementObject ^)enu->Current;
??
What line is the exception occurring on?
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Hi all
Is it a good idea to write cryptography and steganography application in C++.NET
Well i guess .NET already includes the classes for cryptography but i dont think it would be of any good for me as i have to create my final yr project and i dont think its a good idea to use already created classes. So can i create my own classes. Is there any reference over the net for this task .
Or is it better to use C++(BORLAND). Well basically i dont like borland. I want to use C++.NET OR C#.NET and i dont understand VC++ so i cant use that.
So any suggestion would be greatly appreciated.
Thanks
|
|
|
|
|
Software_Specialist wrote: I want to use C++.NET OR C#.NET and i dont understand VC++ so i cant use that.
I'm not sure what you mean here.
I'd just use straight C++, using VS2005. If you're not doing any GUI, then the express edition will work just fine.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
well straight C++..you mean win32 Visual C++. It is bit confusing as in that you have to write your own code for button and other tool box stuff. Is there any proper windows tutorial of VC++ so that i could try that out.
Yeh it gonna be GUI based.
Thanks
|
|
|
|
|
Software_Specialist wrote: you mean win32 Visual C++.
No, I meant for cryptography, you can write on the console and use standard C++. If you want to do a windows program, you should buy VS, as MFC is far better documented. Or, you could use C++/CLI and do the UI in C#, put the crypto code in a dll.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
okay yeh i want to use GUI. But the whole thing was that .NET already provides cryptography classes, cos of that i dont think there would be any link which give an idea to write dt class where as for VC++ there are many articles.
So i guess MFC could help me out in this. Well i never programmed in MFC but ill see if i could get hold of it.
I guess one more thing could be done. if i write the crypto classes in unmanaged C++ and then putting it in dll and then i guess i would be able to import unmanaged dll using dllImport over .NET. Please let me know if it sounds alright.
Thanks a lot
|
|
|
|
|
You can use p/invoke to call methods in a plain DLL from C#.
However, the thing is, if you hope to find an article with code you can use, you may as well use the crypto classes. If you want some help with algorithms, who cares if the code you find is Java, C#, C++ or javascript ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
fair enough..
ill try to work it out..
thanks
|
|
|
|
|
Hi all. Im trying to make a simple DNS client. But its not working the way i had hoped.
Heres my code:
<br />
#include <iostream><br />
#include <winsock.h><br />
using namespace std;<br />
<br />
int main(){<br />
const int SIZE=100;<br />
char msg[SIZE];<br />
printf("Please enter the Host name to look up.\n");<br />
cin.getline(msg, SIZE);<br />
printf("Doing a DNS lookup of '%s'\n",msg);<br />
gethostbyname(msg);<br />
printf("DNS Results are: '%s'",msg);<br />
system("pause");<br />
return 0;<br />
}<br />
Any suggestions?
|
|
|
|
|
use cout instead of printf. use std::string instead of char arrays. Ask in the correct forum in future ( this is for C++/CLI questions ).
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I am new to Managed C code and I am trying to create a packet class to have a socket and buffer array. If I make the class unmanaged I get this error: error C2365: cannot declare a managed 'thisSocket' in an unmanaged 'CSocketPacket'.
If I make the class managed by adding 'public ref class' The program gets like 43 errors instead of just these. My code is below. Please help me. Thanks.
#include "stdafx.h"
#include "Server.h"
using namespace System;
using namespace System::Text;
using namespace System::IO;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Collections;
const int portNumber = 1024;
public class CSocketPacket
{
System::Net::Sockets::Socket thisSocket;
array<byte>^dataBuffer = gcnew array<byte>(1024);
};
// Constructor
Server::Server()
{
}
// Destructor
Server::~Server()
{
}
void Server::initServer()
{
socketListener = gcnew Socket( AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp );
IPEndPoint ipLocal = gcnew IPEndPoint ( IPAddress::Any, portNumber );
socketListener.Bind( ipLocal );
socketListener.Listen(1);
socketListener.BeginAccept( new AsyncCallback(connectCallbackFunction), NULL );
}
void Server::connectCallbackFunction(System::IAsyncResult result)
{
socketWorker = socketListener.EndAccept( result );
WaitForData( socketWorker );
SendData( socketWorker );
}
void Server::WaitFordata(System::Net::Sockets::Socket IOsocket)
{
CSocketPacket packet = new CSocketPacket();
packet.thisSocket = IOsocket;
IOsocket.BeginReceive( packet.dataBuffer, 0, packet.dataBuffer->Length, SocketFlags::None, gcnew AsyncCallback(dataReceivedCallbackFunction), packet);
}
void Server::SendData(System::Net::Sockets::Socket IOsocket)
{
// convert object to byte array
byte[] byteData = System::Text::Encoding::ASCII::get( messageToSend );
// send data
IOsocket.Send ( byteData );
}
void Server::dataReceivedCallbackFunction(System::IAsyncResult result)
{
CSocketPacket packet = (CSocketPacket)result->AsyncState;
int size = packet.thisSocket->EndReceive( result );
char[] buffer = gcnew char[size 1];
System::Text::Decoder decoder = System::Text::Encoding::UTF8::get();
int numCharacters = decoder.GetChars( packet.dataBuffer, 0, size, buffer, 0 );
System::String receivedData = gcnew System::String( buffer );
// print to screen
}
|
|
|
|
|
Do you want the class managed or unmanaged?
For unmanaged, the error message is a good clue - you can't have managed members in an unmanaged
class.
You also can't declare/define/create an object in the class declaration like this:
array^dataBuffer = gcnew array(1024);
System::Net::Sockets::Socket I believe does NOT have a default constructor so you'd need another
way to declare the thisSocket member (probably use a managed pointer).
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
I would like the class unmanaged. How would I create the thisSocket and have it inside that class or how would I use a managed pointer to do this? Still a little confused.
|
|
|
|
|
Maybe something like this:
class CSocketPacket
{
public:
gcroot<System::Net::Sockets::Socket ^> thisSocket;
gcroot<array<Byte> ^> dataBuffer;
CSocketPacket();
};
...
CSocketPacket::CSocketPacket()
{
thisSocket = 0;
dataBuffer = gcnew array<Byte>(1024);
}
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Hi All,
I have another problem with calling manage code from dll.
I do the following:
1. Create dll project in the VS2005, add /clr option under project property->C++->general
2. Add header file to the project with interface:
<br />
#define Base_API __declspec(dllexport)<br />
<br />
class BaseI_SqlDbConnection;<br />
class Base_API Base_SqlDbConnection<br />
{<br />
public:<br />
Base_SqlDbConnection(); <br />
BaseI_SqlDbConnection *m_pi;<br />
};<br />
3. Add define of class to the .cpp file. It is the simple wrapper on the SqlClient.
<br />
using namespace System;<br />
using namespace Data;<br />
using namespace SqlClient;<br />
<br />
#ifdef _MANAGED<br />
#pragma managed(push, off)<br />
#endif<br />
BOOL APIENTRY DllMain( HMODULE hModule,<br />
DWORD ul_reason_for_call,<br />
LPVOID lpReserved<br />
)<br />
{<br />
return TRUE;<br />
}<br />
#ifdef _MANAGED<br />
#pragma managed(pop)<br />
#endif<br />
class BaseI_SqlDbConnection<br />
{<br />
public:<br />
BaseI_SqlDbConnection()<br />
: m_SqlDbConnection(gcnew SqlConnection){}<br />
<br />
gcroot<SqlConnection ^> m_SqlDbConnection;<br />
};<br />
Base_SqlDbConnection::Base_SqlDbConnection() : m_pi(new BaseI_SqlDbConnection)<br />
{<br />
}<br />
4. Call the class from MFC application looking like this:
Base_SqlDbConnection test;
With help of debugger, I found that the BaseI_SqlDbConnection()
constructor doesn't want to call and object m_SqlDbConnection could not be created.
I guess that the problem is in the managed code, but I am newbie for this. Please help
me to resolve the problem or point me to the article about this.
|
|
|
|
|
Hi,
I need to capture a screen image, at first point i dont know which functions to use for a simple screen shot... i need some help for that, but to start with i realy need as i saw in other forums, i could capture a number of triangles or polygons for what is being rendered to the screen, and my question is where to start looking for to do this ???
it is also possible to capture the number of colors that is being rendered ?
Thanks in advance
-- modified at 12:35 Saturday 21st April, 2007
|
|
|
|
|
There are articles on screen capture in C#, these are easily modified to use C++/CLI or managed C++. If you're just in the wrong forum, there are articles on doing it in C++, as well ( in fact, I think they C# ones p/invoke Windows C APIs )
Unless you're in a 3D game ( where screen capture is more difficult ), there are no polygons being rendering on the screen, only pixels. If you want the exact number of colors, you can iterate over a captured bitmap and count them.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
thanks christian,
you know which the best functions to do this with C++ ??
|
|
|
|
|
Please ask in the right forum, first. Also, type 'screen capture C++' into google and you'll find tons of articles on it.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hello,
i try for display a file event log list, i use For each;
EventLog^ myLog = gcnew EventLog;<br />
for each (System::Diagnostics::EventLogEntry^ entree in myLog->Entries)<br />
MessageBox::Show(entree->Message);
there are a sam code in C#
System.Diagnostics.EventLog eventLog1;<br />
foreach (System.Diagnostics.EventLogEntry entree in eventLog1.Entries)<br />
{ MessageBox.show(entree.Message);}
There are error: An unhandled exception of type 'System.ArgumentException' occurred in System.dll
Additional information: Log property value has not been specified
Please help me to resolve this great problem
|
|
|
|
|
That is not the same code! The C++/CLI code:
EventLog^ myLog = gcnew EventLog; ,
and the C# code:
System.Diagnostics.EventLog eventLog1;
are different.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
Hi ,
Why this small C++ code not working ?
Unable to Identify: Pl. Help:
******************************************************
#include <iostream>
#include <string>
#include
#include <vector>
using namespace std;
class c
{
string *cc ;
public:
c(const string& s1):cc(new string(s1)){}
c(const c& k)
{
cc = new string(k.get());
}
string get(){ return *cc;}
~c(){delete cc;}
};
/*
class x
{
vector <c*>cc;
public:
}
*/
int main()
{
c kk("bvls");
cout<<kk.get()<<endl;
c b(kk);
cout<<b.get()<<endl;
c="" ck="b;
cout<<ck.get()<<endl;
return" 0;
}
***********************************************
whats="" the="" mistake="" i="" am="" committing?="" how="" to="" overcome="" it..??
thank="" you="" in="" anticipation.=""
prasad.=""
<div="" class="ForumSig">Programming is Understanding
--Stroustroup
|
|
|
|
|
When you ask this again, in the correct forum ( this is the C++/CLI forum ), define 'not working' so people don't have to run your code to work out what's going wrong.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I am using Visual C++ 2005 C++/CLI and I want to sort the columns in my listView. There is a Microsoft article from 01/09/2006 (Article ID 816183) that explains exactly how to do it using the ListViewColumnSorter object but this does not work for me because the line 'private: ListViewColumnSorter^ lvColumnSorter' does not compile. Can anyone direct me to any other articles that may provide insight?
Thanks
Buck
Buck Brown
|
|
|
|