|
Hello,
We are trying to marshal a simple C structure within our C# code. All values are coming across except for doubles (bolded below). A double comes across as junk. Any help you can provide would be appreciated.
Here is our C structure:
<br />
struct other_earn {<br />
short o_id;<br />
char o_desc[12];<br />
char o_tpind[2];<br />
char o_payee[31];<br />
char o_addr1[31];<br />
char o_addr2[31];<br />
char o_addr3[31];<br />
char o_hours[2];<br />
double o_amt1;<br />
short o_dept;<br />
char o_txficas[2];<br />
char o_txficam[2];<br />
char o_txfit[2];<br />
char o_txfuta[2];<br />
char o_txsit[2];<br />
char o_txsuta[2];<br />
char o_txloc[2];<br />
short o_mdt;<br />
char o_wcexempt[2];<br />
}<br />
And here is how we marshal the struct. Note that all fields except the double are marshalled without issue. We have the same problem with all our other structs:
<br />
[StructLayout(LayoutKind.Sequential)]<br />
public struct ROtherEarn<br />
{<br />
public Int16 o_id;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 12)]<br />
public String o_desc;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]<br />
public String o_tpind;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)]<br />
public String o_payee;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)]<br />
public String o_addr1;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)]<br />
public String o_addr2;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)]<br />
public String o_addr3;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]<br />
public String o_hours;<br />
public Double o_amt1;<br />
public Int16 o_dept;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]<br />
public String o_txficas;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]<br />
public String o_txficam;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]<br />
public String o_txfit;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]<br />
public String o_txfuta;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]<br />
public String o_txsit;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]<br />
public String o_txsuta;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]<br />
public String o_txloc;<br />
public Int16 o_mdt;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]<br />
public String o_wcexempt;<br />
}<br />
Thanks,
Josh Lindenmuth
|
|
|
|
|
What do you mean it's not being marshaled correctly?
As in it won't compile?
As in when you try to use the structure it behaves unexpectedly?
Did you try using the MasrhalAs(UnmanagedType.R4) or MarshalAs(UnmanagedType.R8) properties? As I recall, a C double is smaller than a C# double or something to that effect. (The reason I think this is because for some reason I think that a C long is actually 4 bytes while a C# long is 8 bytes)
So try adding a MarshalAs(UnmanagedType.R4) to your Double. (R4 and R8 marshal floating-point numbers, I2, I4, and I8 marshal integers)
|
|
|
|
|
Can i create a function template in c#.if so how to do it?
Nithya
|
|
|
|
|
Only in VS.NET 2005, which is currently in beta 2.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Hi,
In C# code I declared like this:
[DllImport("mydll.dll")]
public static extern int SearchString(out double x);
And I called the function:
double m_vo2Max=1;
SearchString(out m_vo2Max);
The SearchString function is on the mydll.dll. This dll is written in VC++ and the description is here:
#include "stdafx.h"
#include <stdio.h>
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
int WINAPI SearchString (double &X)
{
FILE *F;
char szString[255];
F= fopen("C:\\ParaLimit1.tbf", "rt");
fgets(szString, 255, F);
fclose(F);
return 1;
}
I always have an error : System.NullReferenceException
Additional Information : Object reference not set to an instance of an object.
I checked and found out that the error in the code below:
fgets(szString, 255, F);
Can you help me to solve this error.
Thank you,
|
|
|
|
|
Why are you using C file handling instead of C++ ?
You should check if F is NULL, perhaps that's where the real problem is.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I have a console application and let's say I have another console application (native c++ and I don't have the source) and I want to run that native application with my app like I would start it in cmd.exe.
For example, say my app was named ant.bat, to run it in the console I would type the C:\antroot\ant and it would run and display it's text in that console window.
Now I want to start ant.bat (Process.Start()???) and but have it run in my console window that my managed app is running in. Because if I just use Process.Start() it runs ant.bat in a new console window and closes when execution is complete.
Any ideas?
/\ |_ E X E GG
|
|
|
|
|
Try this:
public static void launch( string name, string workingDir )
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = name;
process.StartInfo.WorkingDirectory = workingDir;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
System.Console.Write(process.StandardOutput.ReadToEnd());
}
You could also throw in process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; , but I don't think it's necessary.
By the way, this also answers the gcc question below.
Matt Gerrans
|
|
|
|
|
Thanks that works just as I like.
I have another question though. If, for example, I wanted to set an environmental variable with my computer I would go like this in the command prompt....
<br />
set JAVA_HOME=C:\jdk1.3.1_16
How would I do this programmicly in C#?
Thank you for your time.
/\ |_ E X E GG
|
|
|
|
|
|
|
How can I FullRowSelect in DataGrid using C#?
Jassim Rahma
|
|
|
|
|
You can use DataGrid.CurrentRowIndex = index
Hugo Migneron
|
|
|
|
|
Hello,
I am using UDP protocol to broadcast a packet on the network. After I send this packet, some devices on the network answer with a UDP datagram packet containing some information.
My problem is that whenever one of these devices answer, (I know I get the packet because the Socket.Receive function returns me the data I am waiting for) I don't know who this packet is coming from. I am looking for a way to know the IP Address of the packet sender.
This is my code :
<br />
IPEndPoint Broadcast = new IPEndPoint(IP, 2000);<br />
EndPoint BroadcastEP = (EndPoint)Broadcast;<br />
<br />
socket = new Socket(System.Net.Sockets.AddressFamily.InterNetwork,<br />
SocketType.Dgram, ProtocolType.Udp);<br />
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);<br />
socket.SendTo(msg, 0, msg.Length, SocketFlags.None, BroadcastEP);<br />
Then, if I use the socket.Receive function, I get the payload of the packet but not the header information. Is there a way to get this information (Source's IP Address) with System.Net.Sockets or with System.Net.Sockets.UpdClient ?
I thought I could get this information by using socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.PacketInformation, ... but I just can't seem to figure out how to use it correctly.
If anyone knows how to do this, it would be greatly appreciated!
Hugo Migneron
|
|
|
|
|
Hi
When I run g++ (to compile) using process.start I get the error: "g++.exe: Main.cpp: No such file or directory"
If I run g++ using the same arguments from command prompt it runs fine. whats going wrong?
cheers
|
|
|
|
|
|
|
You need to specify the full path of the Main.cpp file when you run it using Process.Start, or, you need to set the current directory to the directory where Main.cpp resides before starting the process.
This is because a process spawned using Process.Start by default, has its current directory set to the application directory of the parent process.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Thanks a lot! How can I "set the current directory to the directory where Main.cpp resides before starting the process"
Cheers again
|
|
|
|
|
You can use the ProcessStartInfo's WorkingDirectory property.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
|
Oops, sorry Senthil, I didn't see your reply to yourself where you already pointed this out!
Matt Gerrans
|
|
|
|
|
|
i have managed to make designcollection to work but i don't know how to create controls with the collection editor.
I have a Item, ItemCollection, ItemCollectionDesigner
an Item consist of text , int and picture
i want to attach a picturebox to it so that it auto generate on my usercontrol...
|
|
|
|
|
I was wondering if anyone can point me in the right direction to replicate the Internet Explorer-style Menu Bar in C# using Windows Forms?
I can easily create the menu using the toolbar control, but I run into problems trying to size the text correctly without the icon. Either I get too much space on the top (setting the toolbar TextAlign to Underneath), or I get too much space to the left (setting to Right). How do I get rid of the space?
~tekun
|
|
|
|