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 / C# / June 2007

Tip: Looking for answers? Try searching our database.

Newbie: API function call with array pointer as argument

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
zhilko@ufacom.ru - 11 Jun 2007 13:43 GMT
Hi,
I'm from C/C++ world and now having troubles converting my old C++
code to C#

The OpenGL function is declared as
void glMultMatrixd( const GLdouble *m );
In C++, I use the code

//-------------------class definition
class CMatrix
{
public: double xx, yx, zx, x0;
     double xy, yy, zy, y0;
     double xz, yz, zz, z0;
     double px, py, pz, w1;
....
}

//--------------------using
CMatrix myMatrix
glMultMatrixd(&myMatrix.xx)

For C# I'm using the OpenGL wrapper from
http://nehe.gamedev.net/counter.asp?file=files/basecode/nehegl_csharp.zip

I'm trying to use it in such way:
//------------------------definition
namespace MyFormProject
{
    public class Matrix
    {
        public double  xx, yx, zx, x0,
                        xy, yy, zy, y0,
                    xz, yz, zz, z0,
                    px, py, pz, w1;
....
      }
}

// -----------------------------wrapper imports OpenGL functions
[DllImport(GL_DLL,EntryPoint ="glMultMatrixd")]
public static extern void glMultMatrixd(double[] m);

// ----------------------using
private unsafe void Draw(Matrix m)
{
   fixed (double* d = &m.xx)
   {
       GL.glMultMatrixd (d); // compiler error!
   }
}

This causes the compiler error "Cannot convert double * to double []".
How to do it correctly?

Thanx in advance
Roman.
Nicholas Paldino [.NET/C# MVP] - 11 Jun 2007 15:46 GMT
You will have to declare the Open GL function as unsafe to allow the use
of pointers:

[DllImport(GL_DLL,EntryPoint ="glMultMatrixd")]
public static unsafe extern void glMultMatrixd(double* m);

   You might want to be careful, if the function is expecting yx to come
after xx in memory, zx after yx, etc, etc, then you aren't going to be able
to get this to work, as it is not guaranteed that those members will be
stored in that order in memory.

   You will have to copy the values to an array (so that you can guarantee
that it is contiguous) and then pass that.

   Either that, or store them in an array in your class to begin with.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi,
> I'm from C/C++ world and now having troubles converting my old C++
[quoted text clipped - 53 lines]
> Thanx in advance
> Roman.
Ben Voigt [C++ MVP] - 11 Jun 2007 18:41 GMT
>    You will have to declare the Open GL function as unsafe to allow the
> use of pointers:
[quoted text clipped - 6 lines]
> able to get this to work, as it is not guaranteed that those members will
> be stored in that order in memory.

With .NET, you can, using the FieldOffset function.  Far better though, to
use an actual array, and define properties as convenience accessors.

The C++ code was totally broken.

>    You will have to copy the values to an array (so that you can guarantee
> that it is contiguous) and then pass that.
[quoted text clipped - 58 lines]
>> Thanx in advance
>> Roman.
Nicholas Paldino [.NET/C# MVP] - 11 Jun 2007 18:56 GMT
Ben,

   Yes, or use the StructLayout attribute (which would be needed with the
FieldOffset attribute as well), initialized to to LayoutKind.Sequential.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>>    You will have to declare the Open GL function as unsafe to allow the
>> use of pointers:
[quoted text clipped - 74 lines]
>>> Thanx in advance
>>> Roman.

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.