Pointers and references VS2005

  • Thread starter Thread starter Rick
  • Start date Start date
R

Rick

Hi guys!!

just one question, can i send pointers from VC++ 2005 to a vc++ 6.0 dll?

if it is possible, how can i do this? does VS2005 have rules to send
pointers?

Regards.
 
Hi guys!!
just one question, can i send pointers from VC++ 2005 to a vc++ 6.0 dll?

if it is possible, how can i do this? does VS2005 have rules to send
pointers?

Regards.

It all depends on what you are trying to do.
First of all: is your VC2005 app a native C++ program? If it is then you can
pass pointers from VC2005 to VC6.0 as long as the pointers point to plain
data or classes than do not use stl, MFC or atl.

For example, passing a char * is no problem. passing an MFC CString * will
not work.

Another rule is that you should never try to delete memory in your app if it
was deleted in the dll or the other way around. that is a good way to crash
your app because the dll and your app use different runtimes.

If you are using a .NET application and a VC6 dll then you have to use a
pin_ptr.

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
oki doki
pin_ptr, im using VC++ managed code, i've passed an int and it works but
when the .Net app executes the dll then i got this error (the value was
changed as i expected),

System.AccessViolationException was unhandled
Message="Attempted to read or write protected memory. This is often an
indication that other memory is corrupt."
Source="SknrMV"
StackTrace:
at ConServ(Int32** )
at SknrMV.Form1.button1_Click(Object sender, EventArgs e) in
c:\programacion\c++net\sknrmv\sknrmv\form1.h:line 133
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at main(String[] args) in
c:\programacion\c++net\sknrmv\sknrmv\sknrmv.cpp:line 16

the value is ok, the dll changes the value at memory address but when .Net
takes control again it crashes =(

Rick.
Regards.
 
Rick said:
oki doki
pin_ptr, im using VC++ managed code, i've passed an int and it works but
when the .Net app executes the dll then i got this error (the value was
changed as i expected),

could you show the bit of code where you pin_ptr the int and call the dll
function?

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
well i'm not using pin_ptr yet, im using this

namespace SknrMV {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Runtime::InteropServices;
[DllImport("C:\\Dll32mv\\Debug\\Dll32din.dll")]
extern "C" void ConServ(int **);
.....
more code and

private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e) {
int *liEstado;
int OtherVal=8;
ConServ(&liEstado); // This is the dll
//OtherVal+= *liEstado;
liEstado=NULL;

}

if i debug the app, i can see how the dll changes the value of &liEstado but
if i try to read the value for example

OtherVal+= *liEstado;

i get the error =(
 
[DllImport("C:\\Dll32mv\\Debug\\Dll32din.dll")]
extern "C" void ConServ(int **);

You are using an int **, not an int*. Do you have any idea what the dll is
trying to do there?
int *liEstado;
int OtherVal=8;
ConServ(&liEstado); // This is the dll
//OtherVal+= *liEstado;
liEstado=NULL;

You are dereferencing a pointer of which you don't know what it is pointing
to. You didn't initialize it. Do you you what happened with it?

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
well i'm not using pin_ptr yet, im using this
namespace SknrMV {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Runtime::InteropServices;
[DllImport("C:\\Dll32mv\\Debug\\Dll32din.dll")]
extern "C" void ConServ(int **);
.....
more code and

private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e) {
int *liEstado;
int OtherVal=8;
ConServ(&liEstado); // This is the dll
//OtherVal+= *liEstado;
liEstado=NULL;

}
Hi,

I just tried the following:
VC6 dll:
extern "C"
{
_declspec(dllexport) void _cdecl fun(int** handle);
}
void fun(int** handle)
{
*handle = new int;
**handle = 8;
//yes i know this is a memory leak. it is just an example
}

VC2005 mixed mode WinForms application (/clr):
#pragma unmanaged
extern "C"
{
_declspec(dllimport) void fun(int** handle);
}
#pragma managed

private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
int * ptr;
pin_ptr<int*> pptr = & ptr;
fun(pptr);
MessageBox::Show((*ptr).ToString());
}

added vc6dll.lib as a linker input, then built the app and pressed button1.
That shows me the messagebox with text '8'. so if you use the pin_ptr it
should work unless the dll is doing something fishy.

I don't see why it should take an int** if all it wants to do is to give you
an int value. a simple int* would have sufficed for that.

have you checked the documentation for your dll function?

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
okekis!!

let me try with this sample and i'll tell you what happened

=)

Rick.
Regards.


Bruno van Dooren said:
well i'm not using pin_ptr yet, im using this

namespace SknrMV {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Runtime::InteropServices;
[DllImport("C:\\Dll32mv\\Debug\\Dll32din.dll")]
extern "C" void ConServ(int **);
.....
more code and

private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e) {
int *liEstado;
int OtherVal=8;
ConServ(&liEstado); // This is the dll
//OtherVal+= *liEstado;
liEstado=NULL;

}
Hi,

I just tried the following:
VC6 dll:
extern "C"
{
_declspec(dllexport) void _cdecl fun(int** handle);
}
void fun(int** handle)
{
*handle = new int;
**handle = 8;
//yes i know this is a memory leak. it is just an example
}

VC2005 mixed mode WinForms application (/clr):
#pragma unmanaged
extern "C"
{
_declspec(dllimport) void fun(int** handle);
}
#pragma managed

private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
int * ptr;
pin_ptr<int*> pptr = & ptr;
fun(pptr);
MessageBox::Show((*ptr).ToString());
}

added vc6dll.lib as a linker input, then built the app and pressed
button1.
That shows me the messagebox with text '8'. so if you use the pin_ptr it
should work unless the dll is doing something fishy.

I don't see why it should take an int** if all it wants to do is to give
you
an int value. a simple int* would have sufficed for that.

have you checked the documentation for your dll function?

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
Hi Bruno, i cant find vc6dll.lib, i did exactly the same that your example,
it doesn't crashes anymore but the returned value is wrong, i thought maybe
because vc6dll.lib, i've vs2005 and vs 6.0 in the same pc but not that lib,
or should i built it?

Regards.

Bruno van Dooren said:
[DllImport("C:\\Dll32mv\\Debug\\Dll32din.dll")]
extern "C" void ConServ(int **);

You are using an int **, not an int*. Do you have any idea what the dll is
trying to do there?
int *liEstado;
int OtherVal=8;
ConServ(&liEstado); // This is the dll
//OtherVal+= *liEstado;
liEstado=NULL;

You are dereferencing a pointer of which you don't know what it is
pointing to. You didn't initialize it. Do you you what happened with it?

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
Bruno, it works whit this

[DllImport("C:\\Dll32mv\\Debug\\Dll32din.dll")]

#pragma unmanaged

extern "C"

{

_declspec(dllimport) void ConServ(int *);

}

#pragma managed



----------------------------------------------------------

int ptr;

ConServ(&ptr);

MessageBox::Show((ptr).ToString());


Thanks by your time =D

Rick.


Rick said:
okekis!!

let me try with this sample and i'll tell you what happened

=)

Rick.
Regards.


Bruno van Dooren said:
well i'm not using pin_ptr yet, im using this

namespace SknrMV {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Runtime::InteropServices;
[DllImport("C:\\Dll32mv\\Debug\\Dll32din.dll")]
extern "C" void ConServ(int **);
.....
more code and

private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e) {
int *liEstado;
int OtherVal=8;
ConServ(&liEstado); // This is the dll
//OtherVal+= *liEstado;
liEstado=NULL;

}
Hi,

I just tried the following:
VC6 dll:
extern "C"
{
_declspec(dllexport) void _cdecl fun(int** handle);
}
void fun(int** handle)
{
*handle = new int;
**handle = 8;
//yes i know this is a memory leak. it is just an example
}

VC2005 mixed mode WinForms application (/clr):
#pragma unmanaged
extern "C"
{
_declspec(dllimport) void fun(int** handle);
}
#pragma managed

private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
int * ptr;
pin_ptr<int*> pptr = & ptr;
fun(pptr);
MessageBox::Show((*ptr).ToString());
}

added vc6dll.lib as a linker input, then built the app and pressed
button1.
That shows me the messagebox with text '8'. so if you use the pin_ptr it
should work unless the dll is doing something fishy.

I don't see why it should take an int** if all it wants to do is to give
you
an int value. a simple int* would have sufficed for that.

have you checked the documentation for your dll function?

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
Back
Top