|
I know it sounds convoluted ...
I have an existing unmanaged c++ static library which uses gcroot to access some c# classes.
for example:
I have something like this
gcroot <MyCSharpClass *>
somewhere in one of the header files of the unmanaged C++ library.
I am now trying to use a C++/CLI dll which will include this header file but i get the following error:
error C3699: '*' : cannot use this indirection on type 'MyCSharpClass ' compiler replacing '*' with '^' to continue parsing
The unmanaged c++ libary is compiled with /clr:old syntax and the new c++/CLI library is compiled only with /clr. I already have put in a bit of code in the c++/CLI library using the ref class, ^, etc so i cannot (easily) change it to using /clr:old.
My question is: is it even possible to access the gcroot member declared in a unmanaged library from a c++/CLI assembly?
Notes:
1. Although i have the source code for this unmanaged c++ static library, i rather not change its source code since that opens a whole new can of worms.
2. This following info may not be relevant: To avoid a "ambiguous IServiceProvider" error, i had to manually include #include <shobjidl.h> in my stdafx.h file of my C++/CLI dll.
3. I have looked into Fraser's book but could not find any info (Nishant's book is on its way).
bipin pillai
|
|
|
|
|
Nishant's book is very good on explaining using managed and native code. However, I believe "Expert C++/CLI.Net for Visual C++ Programmers" by Marcus Heege (Apress) goes into more detail about this subject. Nevertheless, your scenario is a bit weird, and I haven't seen anything dealing with this yet. However, I haven't completed reading each book yet.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
Please can you help me...I have my code written in C++ and I want to run it in c#(because I want to make a interface in c# for my algorithm), I know that I must make an "dll" in C++, thus I can use my code in C# but I don't know how to do this "dll" in c++....thanks in advance
lavi
|
|
|
|
|
Look at the C++/CLI Articles[^] there are several examples of making a managed wrapper for native libraries.
led mike
|
|
|
|
|
sorry I didn't worked with something like this and my time is short...can you tell me if this code is good for creating an "dll",I know that it create shell32.dll but, is this good for creating me a dll in c++ for my algorithm so I can run it in c#
/ / dllsharp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices;
// Declare the function that is exported from unmanaged dll (shell32.dll).
[DllImport("shell32.dll")]
extern "C" int _cdecl ShellExecute(int hwnd, // Handle to a parent window.
String *strVerb, // Action to be performed.
String *strFileName, // File or object on which to execute the specified verb.
String *strParameters, // Parameters to be passed to the application.
String *strDirectory, // Default directory.
int nShowCmd); // Flags.
// Managed class demonstrates Runtime's Platform Invocation Service
// (P/Invoke) to call unmanaged code from managed code.
__gc class Launcher
{
public:
// Starts program that specified by strFileName parameter
static int StartProgram(String *strFileName)
{
return ShellExecute(0,
S"Open",
strFileName,
String::Empty,
String::Empty,
1 /*SW_SHOWNORMAL*/);
}
};
// This is the entry point for this application
int main( int argc, char *argv[ ])
{
// Check parameters.
if(argc < 2)
{
Console::Write(S"Not enough parameters.");
return 0;
}
// Call static function of the Launcher class and start program.
if(Launcher::StartProgram(new String(argv[1])) < 33)
Console::Write(S"Couldn't launch the program!");
return 0;
}
|
|
|
|
|
my time is short man!
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
k...forget about that...can you answer me to this question please...if I run the code above wy it give me this error:
Error 1 fatal error C1190: managed targeted code requires a '/clr' option
|
|
|
|
|
You need to enable CLR in your compiler options.
In VS2003 .NET it's at Project Properties->General->Use Managed Extensions
In VS2005 it's at Project Properties->Configuration Properties->General->Common Language Runtime Support
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Mark Salsbery wrote: my time is short man!
Yes the famous Short Stack Software Development Process introduced by the well known Technology Expurts from The International House of Pancakes.
led mike
|
|
|
|
|
led mike wrote: Yes the famous Short Stack Software Development Process introduced by the well known Technology Expurts from The International House of Pancakes.
My time is short too! Tell the short order cook at IHOP that I'll take a stack. No, make it a queue.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
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
|
|
|
|