468,733 Members | 1,611 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,733 developers. It's quick & easy.

Pointers and references VS2005

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.
Mar 1 '06 #1
9 1282
> 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.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 1 '06 #2
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(MouseEventAr gs 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.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoop(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:li ne 16

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

Rick.
Regards.
"Bruno van Dooren" <br**********************@hotmail.com> escribió en el
mensaje news:uE**************@TK2MSFTNGP14.phx.gbl...
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.
br**********************@hotmail.com
Remove only "_nos_pam"

Mar 1 '06 #3

"Rick" <el*******@hotmail.com> wrote in message
news:ul**************@TK2MSFTNGP12.phx.gbl...
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.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 1 '06 #4
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 =(


"Bruno van Dooren" <br**********************@hotmail.com> escribió en el
mensaje news:%2****************@TK2MSFTNGP14.phx.gbl...

"Rick" <el*******@hotmail.com> wrote in message
news:ul**************@TK2MSFTNGP12.phx.gbl...
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.
br**********************@hotmail.com
Remove only "_nos_pam"

Mar 2 '06 #5
[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.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 2 '06 #6
> 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.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 2 '06 #7
okekis!!

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

=)

Rick.
Regards.
"Bruno van Dooren" <br**********************@hotmail.com> escribió en el
mensaje news:FC**********************************@microsof t.com...
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.
br**********************@hotmail.com
Remove only "_nos_pam"

Mar 2 '06 #8
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" <br**********************@hotmail.com> escribió en el
mensaje news:up**************@TK2MSFTNGP10.phx.gbl...
[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.
br**********************@hotmail.com
Remove only "_nos_pam"

Mar 2 '06 #9
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" <el*******@hotmail.com> escribió en el mensaje
news:%2****************@TK2MSFTNGP11.phx.gbl...
okekis!!

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

=)

Rick.
Regards.
"Bruno van Dooren" <br**********************@hotmail.com> escribió en el
mensaje news:FC**********************************@microsof t.com...
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.
br**********************@hotmail.com
Remove only "_nos_pam"


Mar 2 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Web Developer | last post: by
4 posts views Thread by cppaddict | last post: by
458 posts views Thread by wellstone9912 | last post: by
2 posts views Thread by gnassar | last post: by
7 posts views Thread by Erdal Mutlu | last post: by
6 posts views Thread by Andrew Rowley | last post: by
64 posts views Thread by Zytan | last post: by
19 posts views Thread by MQ | last post: by
29 posts views Thread by Simon Saize | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.