|
//CONSTANTS
public const int IS3K_SHORT_DESCRIPTION_MAX = 20 ;
public const int IS3K_LONG_DESCRIPTION_MAX = 50 ;
public const int IS3K_LINE_KEY_MAX = 12 ;
public const int IS3K_LINES_PER_SHOW = 20 ;
//STRUCT DECLARATION
[StructLayout(LayoutKind.Sequential)]
public struct _IS3kLineDescription
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst= IS3K_LINE_KEY_MAX , ArraySubType = UnmanagedType.U1)]
public byte[] LineKey;
[MarshalAs(UnmanagedType.ByValArray, SizeConst= IS3K_SHORT_DESCRIPTION_MAX , ArraySubType = UnmanagedType.U1)]
public byte [] LongDescription;
[MarshalAs(UnmanagedType.ByValArray, SizeConst= IS3K_LONG_DESCRIPTION_MAX , ArraySubType = UnmanagedType.U1)]
public byte [] ShortDescription;
}
[StructLayout(LayoutKind.Sequential)]
public struct _IS3kShowLines
{
public int Command;
public int Flag;
public int TotalLines;
public int TopLine;
[MarshalAs(UnmanagedType.ByValArray, SizeConst= IS3K_LONG_DESCRIPTION_MAX , ArraySubType = UnmanagedType.Struct) ]
public _IS3kLineDescription[] Line;
};
//Implementaion code
_IS3kShowLines showLines = new _IS3kShowLines() ;
_IS3kLineDescription oneLineDescription = new _IS3kLineDescription();
int x,y;
//this one is working ok
y = Marshal.SizeOf(oneLineDescription );
//this one does not,
//as there is array of struct within struct
//program dies.
//An unhandled exception of type 'System.ArgumentException' occurred in
//Additional information: Type _IS3kShowLines can not be marshaled as
//unmanaged structure; no meaningful size or offset can be computed.
x = Marshal.SizeOf(showLines );
Can anyone please tell why second SizeOf does not work
my intention is find out sizeof showLines variable
thanks
Uday Shastri
|
|
|
|
|
Udaynet wrote:
//this one does not,
//as there is array of struct within struct
x = Marshal.SizeOf(showLines );
Can anyone please tell why second SizeOf does not work
That doesn't help. Please be specific. I suspect you're getting an ArgumentException stating that no meaningful information can be retrieved (or something to that effect).
Also, _IS3kShowLines does not need to be declared as unsafe : not only are you not using unsafe code, unsafe code is not necessary in .NET; though, it's useful for times when you want efficient access to direct memory, like pixel processing for large images. This makes your code unverifiable which may or may not lead to deployment problems (based on your deployment scenario) because of .NET CAS (code access security).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart
Hello
To be more specific I get this error
An unhandled exception of type 'System.ArgumentException' occurred in Myapp.exe
Additional information: Type _IS3kShowLines can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
I have removed unsafe I get the same error.
I have updated my post if you want to refere back to it.
Thanks
Uday Shastri
|
|
|
|
|
I wasn't saying that getting rid of unsafe would help, only that it's completely unnecessary and forces you to compile with /unsafe, which means your assembly is unverifiable (which poses additional restrictions depending on the deployment scenario).
While marshaling structs within structs is supported, there are known problems that have been discussed elsewhere on the 'net. Since you can already determine the size of the structs, it is faster to just hard-code the values as consts. Marshal.SizeOf isn't necessary when you know the size of a struct.
public const int _IS3kLineDescriptionSize = 82;
public const int _IS3kShowLinesSize = 1656; I'd recommend just going that route. It certainly solves the problem and, as I said earlier, is faster than calculating the size of the structs when you already know their size.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi. I am trying to upload and download files to and from my web server. I Keep getting a 405 with WebClient. I understand the problem is that i don't have access. I tried to send a username password with WebClient.Credentials, but it still doesn't work. Is there any way to get permission from a web server to upload a file and then upload it?
-- Steve
|
|
|
|
|
Yes - by using the Credentials property and assigning an ICredentials implementation. If you're using CredentialCache.DefaultCredentials , then it probably won't work unless you're connecting to a site that requires NTLM authentication with your domain account (which is about all that would be in the DefaultCredentials s).
Instead, create a new instance of the CredentialCache to hold multiple credentials, or just a NetworkCredential to hold a single credential.
If you look at the class documentation for the CredentialCache class in the .NET Framework SDK (installed by default with VS.NET), it even gives an example of using both classes mentioned above. For the former, you instantiate a new NetworkCredential and add it to a CredentialCache associated with a given Uri and specifying the authentication type ("Digest", "NTLM", "Kerberos", etc.).
If you wanted to download a file from www.domain.com with username:password, you'd do something like this:
WebClient client = new WebClient();
CredentialCache cache = new CredentialCache();
cache.Add(new Uri("http://www.domain.com/"), "Basic",
new NetworkCredential("username", "password", "DOMAIN")));
client.Credentials = cache;
client.DownloadFile("http://www.domain.com/file.txt", "file.txt"); Read the documentation for NetworkCredential to see what password schemes are supported. You can add additional schemes, if necessary, by implementing the IAuthenticationModule , also - of course - documented in the .NET Framework SDK.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hey. I'm still having problems with uploading. I am using a linux web server. do you think that is causing the trouble? If so, what can i do to get it working?
-- Steve
|
|
|
|
|
You're code uses the HTTP protocol, which is a specification that is independent of the host OS; so, no, it doesn't matter that you're running linux on the server.
If you're having problems, please be specific. The code I gave you is a sample that you should research further, like reading the method and property documentation in the .NET Framework SDK if you don't understand a particular line of code. You should also read - which I mentioned - which authentication protocols are supported. If your web server (presumably Apache) is uses an unsupported authentication mechanism, then you're have to implement the IAuthenticationModule interface and roll your own authentication module.
With specifics regarding your problem, I really can't help you.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi
I have a VC++ program which has a struct defined. I am trying to declare the same struct in c# and am having some problem
--In VC++ , lets say my struct looked like
#define MAX_UNITS 100
struct _MyStruct{
int X;
int Y;
char Units[MAX_UNITS];
};
I can do a sizeof(_MyStruct) and it return me the size including the 100 char array in the struct
--In C#, i tried to declare the following
// all in an unsafe class
struct _MyStruct{
int X;
int Y;
char [] Units = new char[MAX_UNITS];
};
//gives me compile time error about initialising in the struct
so I tried;
struct _MyStruct{
int X;
int Y;
char [] Units;
};
but now i cant use sizeof, and if i use the marshal.sizeof, it returns 12. I need the size with the 100 char array. Any suggestions
thanks
|
|
|
|
|
You are not allowed do field initialization in structs. If you want this behavior you have to use a class. Or if you are inclined you can wait to initialize that field later.
|
|
|
|
|
If you want, you can define a non-default constructor that sets fields to the parameters you specify, and initializes the other fields to whatever you want. You cannot define a default constructor (i.e., a constructor with no params) for a struct, though.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I am too embarrassed to ask this silly question. But I need help. Can any1 tell me, how can I access between forms in C#? For some weird reason I can’t access. I created an object of the form I am trying to access but it’s referring to null.
Say, i have got two forms name "formA" and "formB". I have got five buttons in "formA". How can i access those buttons from formB.
N.B. All the button got public access.
If anyone could help, it would be greatly appreciated. I could attach sample code but i can't see any options here. So, if u give me e-mail then i could send it to ya.
Ta
B
|
|
|
|
|
Well Forms.Controls is public and should be available to anyone outside the "formA" object. I'm kind of confused how you don't have access to either this or the individual controls if you claim they are all public.
|
|
|
|
|
To add to what Tom said. You should not declare fields in C# as public, you should declare them as private or protected and then provide public properties to access them. Declaring fields as public breaks the concept of encapsulation in Object Orientation.
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
|
|
|
|
|
|
Mehbub wrote:
I could attach sample code but i can't see any options here
Try pasting the code section where you create the form and it is refering to null.
<font=arial>Weiye Chen
Life is hard, yet we are made of flesh...
|
|
|
|
|
Ok, i have received your code. Let me paste some of it here so that the rest of the developers can give their advice too.
FormA.cs
private void Form1_Load(object sender, System.EventArgs e)
{
Form frmB = new FormB();
frmB.MdiParent = this;
frmB.Show();
}
FormB.cs
private void btnAccessFormA_Click(object sender, System.EventArgs e)
{
Form frmA = new FormA();
}
Mehbub wrote:
How can i access those buttons from formB.
You can't access the same form A's buttons in form B by creating Form A again. Since FormA is your mdi parent and FormB is the child, you can use the MdiParent property of FormB. e.g.
FormB.cs
private void btnAccessFormA_Click(object sender, System.EventArgs e)
{
this.MdiParent.button1.Visible = true;
} I'm not sure if you need to cast it to FormA first like :
((FormA)this.MdiParent).button1.Visible = true; I don't have VS.NET on my pc here to test it out.
<font=arial>Weiye Chen
Life is hard, yet we are made of flesh...
|
|
|
|
|
Lately I've been getting pretty excited with Interop stuff, but I have one problem, I have no idea what the parameters for a particular .dll method are. I know how to view all the exported functions in a method using DUMPBIN or LINK (if anyone knows a better way, I'm all ears) but it doesn't tell me anything about the parameters. Any ideas?
|
|
|
|
|
If you're talking about native DLLs (which you most likely are since you're viewing the EAT with DUMPBIN), this is not even possible. The exported functions are merely RVAs (relative virtual addresses). The execution stack contains pointers or data that the function call pops from. The parameters are not known except by the function itself, so they are not in the EAT (export address table).
All you can do is consult the documentation. If these are Windows DLLs, most APIs are documented in the MSDN Library[^], which should be your first stop for any questions about Windows platform developer (contains the Platform SDK, the .NET Framework SDK, Web Development, and many other Windows-related documentation).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
If you're talking about native DLLs (which you most likely are since you're viewing the EAT with DUMPBIN), this is not even possible.
That's really good to know. Thanks a ton Heath. Looks like you're answering everybody's questions today.
|
|
|
|
|
Hi all,
I'd like to know how would i be able to code an application to copy the time (copy a time stamp) from a client computers clock.
Example Program:
A simple windows application with a button on it,
when i click the button,
the client computer returns a time stamp or time strap from the computer's clock to the form "04:17pm", to a label or textbox.
The source code would be very much appreciated ..!
Thanx!
|
|
|
|
|
Logically, you should look at the DateTime structure in the .NET Framework SDK, which has a static property called Now . If you further look at the documentation for ToString , the solution's quite obvious:
textBox1.Text = DateTime.Now.ToString("hh:mmtt");
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I would like to share a solution with all of you guys to the Response.Redirect problem.
I have a scenario:
1. An asp page containing two submit type buttons 'Back' and 'Save'.
2. Save may do some action and stay in the same page..
3. But Back would do same as save button..but Goes to Home Page.
4. U may have Save_Click () serverside code in aspx.cs
5. U can simulate the same from back.. and also hidBack to maintain from which button its coming from...
<input type="image" id="Button2" style="Z-INDEX: 101; LEFT: 304px; POSITION: absolute; TOP: 136px"
value="Back" OnClick="return ClickBack()">
<script language="javascript">
function ClickBack()
{
document.Form1.hidBack.value = "YES";
document.Form1.Button1.click();
//IMPORTANT:: RETURN FALSE TO NULLIFY POSTING BY BACK BUTTON.
return false;
}
6. Server side code may look as
private void Save_Click(object sender, System.EventArgs e)
{
if(Request.Form["hidBack"] == "YES")
{
Page.Response.Redirect("Home.aspx",false);
}
Response.Write(" U pressed first Button");
}
</script>
7.if WE DONT HAVE //IMPORTANT:: code line, it will postback twice to the server..there by Response.Redirect doesnt work.
8. Any other observations?? thanks
Phani, Virtusa.
|
|
|
|
|
|
I am creating a web page in C# using XML and XSLT. I am able to access the web controls but I am unable to access the 'file' control.
I am using the code in C# like this
XPathDocument ctrlCategoryDoc = new XPathDocument(Server.MapPath("XML/NewCategory.xml"));
XPathNodeIterator itr = ctrlCategoryDoc.CreateNavigator().Select("//Ctrl");
System.Text.StringBuilder sb;
sb = new System.Text.StringBuilder();
// foreach Ctrl
while (itr.MoveNext())
{
// get the control name
string controlName = itr.Current.GetAttribute("name", "");
sb.Append(controlName);
sb.Append(" : ");
// get the control
object ctrl = FindControl(controlName);
}
the object 'ctrl' contains name of the control in case of web control but in case of html control () it is giving undefined value.
can anyone tell me the solution for this
|
|
|
|