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++ / August 2007

Tip: Looking for answers? Try searching our database.

COM problems

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dragonslayer008@hotmail.com - 12 Aug 2007 19:13 GMT
I am trying to initialize Direct3D 9 in a C++/CLR application.
However, the create device function gives me a compilation error:

error C2664: 'IDirect3D9::CreateDevice' : cannot convert parameter 6
from 'cli::interior_ptr<Type>' to 'IDirect3DDevice9 **'
1> with
1> [
1> Type=IDirect3DDevice9 *
1> ]
1> Cannot convert a managed type to an unmanaged type

The code giving the error:

HRESULT hr;
hr = pd3dObject->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, pd3dPP, &pd3dDevice);

I thought a managed C++ class can have pointers to unmanaged code?
dragonslayer008@hotmail.com - 12 Aug 2007 20:19 GMT
Hmm I managed to get this error to go away by doing the following:

I put the code in an unmanaged class UC.  Then I added a pointer to UC
to a managed class.  Any explanation of why this works?
Ben Voigt [C++ MVP] - 13 Aug 2007 18:34 GMT
> Hmm I managed to get this error to go away by doing the following:
>
> I put the code in an unmanaged class UC.  Then I added a pointer to UC
> to a managed class.  Any explanation of why this works?

Because when the pointer was inside the managed class, it would be moved
around by the garbage collector.  Now it's in the native heap and won't be
moved.

pin_ptr would have worked equally well.
dragonslayer008@hotmail.com - 13 Aug 2007 00:08 GMT
On Aug 12, 11:13 am, dragonslayer...@hotmail.com wrote:
> I am trying to initialize Direct3D 9 in a C++/CLR application.
> However, the create device function gives me a compilation error:
[quoted text clipped - 15 lines]
>
> I thought a managed C++ class can have pointers to unmanaged code?

After more experimenting, I found that this works:

pin_ptr<IDirect3DDevice9*> p = &pd3dDevice;

HRESULT hr;
hr = pd3dObject->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
(HWND)hwnd.ToPointer(),
    D3DCREATE_SOFTWARE_VERTEXPROCESSING, pd3dPP, p);

Why though?  Isn't pd3dDevice (which is a data member of a ref class)
unmanaged since I'm using * syntax?  I don't see why I have to pin
down an unmanaged pointer...
Ben Voigt [C++ MVP] - 13 Aug 2007 18:37 GMT
> On Aug 12, 11:13 am, dragonslayer...@hotmail.com wrote:
>> I am trying to initialize Direct3D 9 in a C++/CLR application.
[quoted text clipped - 29 lines]
> unmanaged since I'm using * syntax?  I don't see why I have to pin
> down an unmanaged pointer...

Because the address of any data, no matter whether it's an integer, managed
handle, or unmanaged pointer, which is a member of a managed data type can
change because .NET uses a compacting garbage collector.

So you tell D3D, create a device, put the pointer to it *here*... but by the
time D3D gets done, *here* has changed and D3D doesn't know it.
Ben Voigt [C++ MVP] - 13 Aug 2007 18:39 GMT
> On Aug 12, 11:13 am, dragonslayer...@hotmail.com wrote:
>> I am trying to initialize Direct3D 9 in a C++/CLR application.
[quoted text clipped - 29 lines]
> unmanaged since I'm using * syntax?  I don't see why I have to pin
> down an unmanaged pointer...

This would work too:

IDirect3DDevice9* tempDevice;
HRESULT hr;
hr = pd3dObject->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
(HWND)hwnd.ToPointer(), D3DCREATE_SOFTWARE_VERTEXPROCESSING, &tempDevice,
p);
pd3dDevice = tempDevice;

Because variables on the stack don't take part in compaction.

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.