|
I want to know the difference between managed and unmanged code..
Thanks
vikas da
|
|
|
|
|
|
Hi,
I have a C++ library app which talks to a C++ server and I am creating a vectorje of my custom class objects. But my Cpp/CLI app(which interacts with native C++ ), throws a memory violation error when I try to return my custom class obj vector.
Code Sample -
In my native C++ class -
std::vector<a> GetStuff(int x)
{
-- do stuff
std::vector<a> vec;
A a;
vec.push_back(a);
--- push more A objs
return vec;
}
In my Cpp/CLI class
public void doStuff()
{
std::vector<a> vec;
vec = m_nativeCpp->GetStuff(4); // where nativeCpp is a dynamically allocated class in nativecpp DLL, the app throws up a memory violation error here!
}
Anything I am missing here ?What should be the ideal way to return such an array -
Regards
Amit </a></a></a>
|
|
|
|
|
Type A is created on stack and this may be the problem. Have you tried to allocate it on heap and let vector hold a pointer to it. I believe that will solve the issue. I haven't tried this, but a wild guess.
Member 5703905 wrote: throws up a memory violation error
What is the exact error message?
|
|
|
|
|
Okay, but I wanted to convey that nativeC++ DLL and Cpp/CLI DLL are in diff projects. Not sure if it is a nice idea to return a ptr from unmanaged project to managed cpp/cli project.
N a v a n e e t h wrote: What is the exact error message?
An unhandled exception of type 'System.AccessViolationException' occurred in CLIConsole.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Thanks
Amit
|
|
|
|
|
Member 5703905 wrote: Not sure if it is a nice idea to return a ptr from unmanaged project to managed cpp/cli project.
It's fine in C++/CLI. Native objects will be on the native heap, managed objects
will be on the managed heap. If you need to hold the pointer in a managed object, you
can use the IntPtr type.
If you expect to allocate the native object in one DLL and free it in another DLL, you'll
want to make sure both DLLs are sharing the same CRT library (DLL).
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi,
In VC++ I want to execute .exe using ShellExecute command. But ShellExecute works for hardcoded file names. Suppose that if I want to pass a file selected by the user i.e.this->openFileDialog1->FileName in ShellExecute, how should I do that?
::ShellExecute(NULL, L"open", L"C:\\play.bat", this->openFileDialog1->FileNameNULL, SW_SHOW);
gives error : cannot convert from 'System::String ^' to 'LPCTSTR'
even using ((LPCTSTR)this->openFileDialog1->FileName)doesn help
|
|
|
|
|
|
HI
I need hook events of keyboard in my app, this is made in managed c++, i did one dll in c++ for hook keyboard and this run good.
first i did simple app in C for test my dll and using SetWindowsHookEx
BOOL HazHook()
{
HOOKPROC hkprcSysMsg;
HPROC hkprcSysMsg2;
HINSTANCE hinstDLL;
HHOOK hhookSysMsg;
hinstDLL = LoadLibrary((LPCTSTR) "keyboard.dll");
if ( hinstDLL != NULL)
{
OutputDebugString ( "1> OK\n");
hkprcSysMsg2 = (HPROC)GetProcAddress(hinstDLL, "_keyboardHookProc@12");
if ( hkprcSysMsg2 != NULL)
{
OutputDebugString ( "2> OK\n");
// LocalhPrevInstance is hPrevInstance of WinMain app.
hhookSysMsg = SetWindowsHookEx(WH_KEYBOARD_LL,hkprcSysMsg2,hinstDLL,LocalhPrevInstance); // original
if( hhookSysMsg != NULL)
{
OutputDebugString ( "3> OK\n");
return TRUE;
}
else OutputDebugString ( "3> FAIL\n");
}else OutputDebugString ( "2> FAIL\n");
}else OutputDebugString ( "1> FAIL\n");
return FALSE;
}
The problems start whemn I need use SetWindowsHookEx in Managad C++, I try inport API but the conversion of rates I not know, HOOKPROC, HINSTANCE , DWORD is unsigned int
[DllImport("user32.dll")]
static int SetWindowsHookEx(int idHook, HOOKPROC lpfn, HINSTANCE hMod,DWORD dwThreadId);
Other try is using unamanged C++
#include "stdafx.h"
#pragma unmanaged
#include <windows.h>
#include <stdio.h>
typedef LRESULT (CALLBACK *HPROC)(int , WPARAM , LPARAM );
bool lalaLoadDllKeyboard()
{
HOOKPROC hkprcSysMsg;
HPROC hkprcSysMsg2;
HINSTANCE hinstDLL;
HHOOK hhookSysMsg;
hinstDLL = LoadLibrary( "keyboard.dll");
if ( hinstDLL != NULL)
{
hkprcSysMsg2 = (HPROC)GetProcAddress(hinstDLL, "_keyboardHookProc@12");
if ( hkprcSysMsg2 != NULL)
{
OutputDebugString ( "2> OK\n");
//hhookSysMsg = SetWindowsHookEx(WH_KEYBOARD_LL,hkprcSysMsg2,hinstDLL,LocalhPrevInstance); // original
hhookSysMsg = SetWindowsHookEx(WH_KEYBOARD_LL,hkprcSysMsg2,hinstDLL,0); // original
if( hhookSysMsg != NULL)
{
OutputDebugString ( "3> OK\n");
return true;
}
}
}
return false;
}
#pragma managed
But to compile errors:
1>------ Operación Generar iniciada: proyecto: WinFormsHookKeyboard, configuración: Debug Win32 ------
1>Vinculando...
1>codeunamanged.obj : error LNK2019: símbolo externo _SetWindowsHookExA@16 sin resolver al que se hace referencia en la función "bool __cdecl lalaLoadDllKeyboard(void)" (?lalaLoadDllKeyboard@@YA_NXZ)
Any idea ??
Thank
|
|
|
|
|
Kar98 wrote: I try inport API but the conversion of rates I not know, HOOKPROC, HINSTANCE , DWORD is unsigned int
pinvoke.net: SetWindowsHookEx()[^]
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hello,
I'm new in Visual C++.I want to convert row elements from two dimensional array to one dimensional arrays.I wrote Visual C++ function to do this.This function has only one input parameter.Input parameter is two dimensional array.Firstly I tried to copy first row elements of input two dimensional array to one dimensional array in this function.One dimensional array will have been output parameter of function.But it didn't work.
The code is below:
// Temp is one dimensional array its length is 512
// Input paremeter is two dimensional array its width is 512 ,height is 512
// I want to copy first row elements of input two dimensional array to one dimensional array.
array^ fonksiyon (array<double,2>^dizi )// The function
{ array^ temp=gcnew array(512);
Array::Copy(dizi,temp,512);
return temp;// return one dimesional array
}
Could you please help me ? Best regards...
|
|
|
|
|
Hey everyone... I need some assistance getting an image from a string.
I have an RTF text box into which a user can paste an image. However, I'd like to take this image out of the RTF and store is elsewhere. The System.Drawing.Image class has a function called FromString but I can't seem to figure out how to correctly use it. I've also looked at the System.Drawing.ImageConverter class but that hasn't helped either.
Can anyone tell me how to get this image out of the rtf and into a bitmap object?
Thanks,
Sam.
|
|
|
|
|
OO... i mean the imageconverter class has a function called "FromString" - not the image class...
|
|
|
|
|
There is a DrawToBitmap[^] method for control. Have you tried that?
Edit:
Aahh, I have seen that documentation[^] saying it is not fully functional with rich textbox.
|
|
|
|
|
i am developing a application which has got a admin module and a user module, admin module will be running on only one machine, but the user modules will be running on multiple systems. i have got some settings to be done in admin module and the same settings should be used by user module also, how to get the settings in the user module from the admin machine. whenever any settings are changed in admin module all the user modules should get the new settings.
please help me out of this
Naveen
modified on Friday, January 30, 2009 2:38 AM
|
|
|
|
|
Hi. I have a synchronous socket listening for tcp connections with a pending size of 1. When a connection request is received, its accepted and processed. But after I am done with that connection and release resources, eventhough the server socket is listening state, it doesn^t accept new connections and blocks forever. What can be the reason?
Also, is there any way to empty the pending queue of a socket?
Thanks
|
|
|
|
|
randomVariable wrote: I have a synchronous socket listening for tcp connections with a pending size of 1
Post that code
randomVariable wrote: it doesn^t accept new connections
What error message does the connecting socket receive?
|
|
|
|
|
The socket is innocent, seems like it was my fault at another part of the code. Anyway, the problem is solved. Thank you very much for your reply.
|
|
|
|
|
May I know what the problem was?
ty
|
|
|
|
|
I have inserted the mschart into window form of visual c++ 2008, but there is very little articles talking about how to use it in c++ , does anyone know how to use it?Thanks.
|
|
|
|
|
Here[^] is a C# implementation. Converting that to C++/CLI will be trivial.
|
|
|
|
|
I have a CLR Application where I am trying to write a .NET wrapper over calls made to native C++ Library. I am using VS 2008 but libraries are compiled in VS2003 or 2005. I am getting some linker errors on linking. Strangely, every error is 1 of these 3 unresolved external symbol ones -
1. std::locale::facet::_Register
2. std::ctype&::_Getcat
3. std::strstream::strstream
Details of the errors -
error LNK2001: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall std::strstream::~strstream(void)" (__imp_??1strstream@std@@UAE@XZ)
error LNK2001: unresolved external symbol "__declspec(dllimport) public: void __thiscall std::locale::facet::_Register(void)" (__imp_?_Register@facet@locale@std@@QAEXXZ)
error LNK2001: unresolved external symbol "__declspec(dllimport) public: void __thiscall std::locale::facet::_Register(void)" (__imp_?_Register@facet@locale@std@@QAEXXZ)
error LNK2001: unresolved external symbol "__declspec(dllimport) public: static unsigned int __cdecl std::ctype<char>::_Getcat(class std::locale::facet const * *)" (__imp_?_Getcat@?$ctype@D@std@@SAIPAPBVfacet@locale@2@@Z)
error LNK2001: unresolved external symbol "__declspec(dllimport) public: static unsigned int __cdecl std::ctype<char>::_Getcat(class std::locale::facet const * *)" (__imp_?_Getcat@?$ctype@D@std@@SAIPAPBVfacet@locale@2@@Z)
error LNK2001: unresolved external symbol "__declspec(dllimport) public: static unsigned int __cdecl std::ctype<unsigned short>::_Getcat(class std::locale::facet const * *)" (__imp_?_Getcat@?$ctype@G@std@@SAIPAPBVfacet@locale@2@@Z)
error LNK2001: unresolved external symbol "__declspec(dllimport) public: static unsigned int __cdecl std::ctype<unsigned short>::_Getcat(class std::locale::facet const * *)" (__imp_?_Getcat@?$ctype@G@std@@SAIPAPBVfacet@locale@2@@Z)
error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall std::strstream::strstream(char *,int,int)" (__imp_??0strstream@std@@QAE@PADHH@Z)
Thanks a lot for the help.
Amit
|
|
|
|
|
I'm getting a few errors while creating a new thread, I don't know what am I doing wrong but somethings not working properly in the Thread creation in the above code.
#include "stdafx.h"
using namespace System;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::IO;
using namespace System::Threading;
using namespace System::Collections;
using namespace System::Text;
ref class ClientHandler
{
public:
TcpClient^ clientSocket;
void RunClient()
{
StreamReader^ readerStream = gcnew StreamReader(clientSocket->GetStream());
NetworkStream^ writerStream = clientSocket->GetStream();
String^ returnData = readerStream->ReadLine();
String^ userName = returnData;
Console::WriteLine("Welcome " + userName + " to the Server");
while (true)
{
returnData = readerStream->ReadLine();
if (returnData->IndexOf("QUIT") > -1)
{
Console::WriteLine("Bye Bye " + userName);
break;
}
Console::WriteLine(userName + ": " + returnData);
returnData += "\r\n";
array<byte>^ dataWrite = Encoding::ASCII->GetBytes(returnData);
writerStream->Write(dataWrite, 0, dataWrite->Length);
}
clientSocket->Close();
}
};
const int ECHO_PORT = 8080;
void main(void)
{
try
{
TcpListener^ clientListener = gcnew TcpListener(ECHO_PORT);
clientListener->Start();
Console::WriteLine("Waiting for connections...");
while (true)
{
TcpClient^ client = clientListener->AcceptTcpClient();
ClientHandler^ cHandler = gcnew ClientHandler();
cHandler->clientSocket = client;
Thread^ clientThread = gcnew Thread(gcnew ThreadStart(cHandler, &cHandler::RunClient));
clientThread->Start();
}
clientListener->Stop();
}
catch (Exception^ e)
{
Console::WriteLine("Exception: " + e);
}
}</byte>
Here are the errors I'm getting
1>.\ThreadedServer.cpp(57) : warning C4947: 'System::Net::Sockets::TcpListener::TcpListener' : marked as obsolete
1> Message: 'This method has been deprecated. Please use TcpListener(IPAddress localaddr, int port) instead.
1>.\ThreadedServer.cpp(74) : error C2825: 'cHandler': must be a class or namespace when followed by '::'
1>.\ThreadedServer.cpp(74) : error C2039: 'RunClient' : is not a member of '`global namespace''
1>.\ThreadedServer.cpp(74) : error C2146: syntax error : missing ')' before identifier 'RunClient'
1>.\ThreadedServer.cpp(74) : error C3924: error in argument #2 of delegate
constructor call 'System::Threading::ThreadStart':
1> pointer to member function expected
1>.\ThreadedServer.cpp(74) : error C2146: syntax error : missing ')' before identifier 'RunClient'
1>.\ThreadedServer.cpp(74) : error C2146: syntax error : missing ';' before identifier 'RunClient'
1>.\ThreadedServer.cpp(74) : error C2059: syntax error : ')'
1>.\ThreadedServer.cpp(74) : error C2059: syntax error : ')'
1>.\ThreadedServer.cpp(74) : error C2065: 'RunClient' : undeclared identifier
Any hints will be really appreciated
|
|
|
|
|
try this
Thread^ clientThread = gcnew Thread(gcnew ThreadStart(cHandler, &ClientHandler::RunClient));
Don't be overcome by evil, but overcome evil with good
|
|
|
|
|
Cool, you just proved that I'm e n00b.
I don't know how but I looked over that line a hundred of times and still couldn't figure it out.
Whatever, problem's solved....
|
|
|
|