473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

PtrToStructure Marshal

Hi everybody.

I have a structure in unmanaged C++:
Expand|Select|Wrap|Line Numbers
  1. struct TLogonParams
  2. {
  3.     char name[100];
  4.     char pswd[100];
  5.     char alias[100];
  6.  
  7.     int  loggedon;
  8.     int  DBType;         
  9.     int  super;           
  10.     int  idUser;          
  11.     char UserName[100];  
  12. };
  13.  
and a function:
Expand|Select|Wrap|Line Numbers
  1. EXPORT_TYPE TLogonParams* __stdcall GetLogonParams(bool AUseDefault = true);
  2.  
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4.  
A would like to call this function from C# and I did this for it:
Expand|Select|Wrap|Line Numbers
  1. public class Hdologon3Csharp
  2. {
  3.       public Hdologon3Csharp()
  4.       {
  5.       }
  6.  
  7. [StructLayout(LayoutKind.Explicit)]    
  8. public class LogonParams
  9. {
  10.     [FieldOffset(0)] public char[] name;
  11.     [FieldOffset(100)] public char[] pswd;
  12.     [FieldOffset(200)] public char[] alias;
  13.  
  14.     [FieldOffset(300)] public int  loggedon;
  15.     [FieldOffset(304)] public int  DBType;
  16.     [FieldOffset(308)] public int  super;
  17.     [FieldOffset(312)] public int  idUser;
  18.     [FieldOffset(316)] public char[] UserName; 
  19. }
  20.  
  21. [DllImport("hdologon3.dll", EntryPoint="GetLogonParams", CharSet = CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]        
  22. public static extern IntPtr GetLogonParams( bool AUseDefault );
  23. public unsafe int getLogonParamsCsharp()
  24. {
  25.     try 
  26.     {
  27.         System.IntPtr iptr = Marshal.AllocHGlobal(416);
  28.  
  29.         iptr = GetLogonParams( true );
  30.  
  31.         object o = (LogonParams)Marshal.PtrToStructure ( iptr, typeof  
  32.                                                                           (LogonParams) );
  33.  
  34.         return 0;
  35.     }
  36.     catch ( Exception e)
  37.     {
  38.         string s = e.Message;                
  39.     }
  40. }
  41.  
I get a value 0 for Iptr and so <undefined> for o.

Can anyone help me with this? I'd be very happy. Thanks.
Mar 2 '07 #1
0 1046

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ken Allen | last post by:
The documentation is not clear on the exact behaviour of the Marshal.PtrToStructure method and whether it copies the contents of the IntPtr region to a new managed object or whether it creates the...
2
by: Andre | last post by:
Hi, I have a question about Marshal.PtrToStructure method. I have a function Func() in unmanaged C++ which returns a pointer to a structure Str which is held on the unmanged site: Str* a =...
2
by: John | last post by:
public const int FRAME_LENGTH=144; public class FRAME_T { public byte Block = new byte; } public void MyDataHandler(IntPtr appContext, IntPtr data) FRAME_T frame = new FRAME_T();...
3
by: Mateusz [PEYN] Adamus | last post by:
Hi First of all please let me know if there is some other, more suitable group for this news - TIA :-) Now, my problem. I'm writing an C# application in which I'm using some functions from...
4
by: C Learner | last post by:
Hi, I have an application which is using a dll written in C++. When the program go to the function below, it would raise an NullReferenceException at the line of PtrToStructure. public bool...
1
by: Jay | last post by:
Hi, In my application, C++ dll is posting some message,which is processed by a form in C# ,where I use Message.Lparam to convert it in structure using Marshal.PtrToStructure() mehtod ,but it...
1
by: spamacon | last post by:
Hello, I have a strange situation using .Net FW 1.1. I want to use Marshal.PtrToStructure to fill the structure below. The first 3 fields get filled correctly: ulStruct describes how big the...
0
by: P Laxmi | last post by:
I have following two structures in vc++ and i want to convert it into c# typedef struct { ULONG CallRef ; ULONG CallSource ; ULONG ...
6
by: carles | last post by:
Hi, Here, sample code where a byte array is used to fill a particular structure: fs = File.OpenRead(path); // FileStream BITMAPFILEHEADER bfh = new BITMAPFILEHEADER(); b = new byte;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.