Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2006

Tip: Looking for answers? Try searching our database.

Pointers and references VS2005

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rick - 01 Mar 2006 19:31 GMT
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.
Bruno van Dooren - 01 Mar 2006 20:13 GMT
> Hi guys!!
>
[quoted text clipped - 4 lines]
>
> 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.

Signature

Kind regards,
   Bruno.
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

Rick - 01 Mar 2006 22:04 GMT
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.

>> Hi guys!!
>>
[quoted text clipped - 19 lines]
> If you are using a .NET application and a VC6 dll then you have to use a
> pin_ptr.
Bruno van Dooren - 01 Mar 2006 22:23 GMT
> 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?

Signature

Kind regards,
   Bruno.
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

Rick - 01 Mar 2006 23:49 GMT
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 =(

>> oki doki
>> pin_ptr, im using VC++ managed code, i've passed an int and it works but
[quoted text clipped - 3 lines]
> could you show the bit of code where you pin_ptr the int and call the dll
> function?
Bruno van Dooren - 02 Mar 2006 05:59 GMT
> [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?

Signature

Kind regards,
   Bruno.
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

Rick - 02 Mar 2006 15:51 GMT
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.

>> [DllImport("C:\\Dll32mv\\Debug\\Dll32din.dll")]
>> extern "C" void ConServ(int **);
[quoted text clipped - 11 lines]
> 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?
Bruno van Dooren - 02 Mar 2006 08:06 GMT
> well i'm not using pin_ptr yet, im using this
>
[quoted text clipped - 20 lines]
>
> }
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?

Signature

Kind regards,
   Bruno.
  bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

Rick - 02 Mar 2006 14:44 GMT
okekis!!

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

=)

Rick.
Regards.

>> well i'm not using pin_ptr yet, im using this
>>
[quoted text clipped - 62 lines]
>
> have you checked the documentation for your dll function?
Rick - 02 Mar 2006 16:08 GMT
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.

> okekis!!
>
[quoted text clipped - 71 lines]
>>
>> have you checked the documentation for your dll function?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.