|
No. If you want an ImageList -like class to hold more, you'll need to develop your own; but I warn you that this would be very resource intensive.
You might consider, instead, to cache those images to disk since they would require a lot of memory, of which the CLR already requires quite a bit plus the more assemblies you use, the more memory is required (this is true of any application that imports libraries).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi all,
i'm tring to write a progarm to manage my network cards (wire/wireless).
I want to disable the wire card and enable the wireless (and reverse).
from my research i understand that wmi is not providing this feature, only to disble DHCP or give 0.0.0.0 in the IP.
i need to diable(and enable)throw c#.
does any one know how ?
pls help ASAP.
10x
liron shryer
Nice systems.
|
|
|
|
|
My console project is named UsingConfigSettings in C#, and wanted to store the database connection in an application config file. I added an application config named "UsingConfigSettings.exe.config" in my project. Here is its contents:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="constring" value="server=localhost;database=Northwind;uid=sa;pwd=;"/>
</appSettings>
</configuration>
I even copied it in the bin\debug folder in my C# console application, but it still doesn't work. I acccess it in the console as:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public class UsingConfigSettings
{
public static void Main()
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["constring"]);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Customers";
cmd.Connection = con;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
Console.WriteLine("{0} - {1}", reader.GetString(0), reader.GetString(1));
}
con.Close();
Console.ReadLine();
}
}
but it won't work, it always issued a message of:
"An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll
Additional information: The ConnectionString property has not been initialized."
If I don't use any config file, and replace the code above, it works fine:
SqlConnection con = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=;");
Please help.
|
|
|
|
|
If you want the .config file to be copied automatically to the target directory, create a file named app.config in the root of your project. VS.NET will automatically rename this according to your executable name and copy it into the bin\Debug or bin\Release directory along with the application.
Make sure that when you run the application, you are running the one in the target directory. If you hit F5 (debug), the application is actually run out of the obj\Debug directory. Using the method I described above will make sure the .config file is renamed and copied to the obj\Debug directory as well (which is actually the target directory).
Everything else you have is right.
FYI, never use sa without a password! Since you're using SQL, either use SSPI (don't specify uid or password, and instead use Integrated Security=SSPI and add your Windows login username or group to SQL Server), or set up a different user account. Give sa a password anyway. This is like having the administrator account with no password as well - it's a big hole in security.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I am wrapping a Dll function with managed code and am confused about the equivalent of pointers and the & symbol in C. I am working with two C functions wrapped in a Dllimport:
[DllImport("msg.dll",CharSet=CharSet.Ansi)]
public static extern int MsgConnect (int lAPIVersion, string lpcszTopic, ref OPERATOR lpOperator, ref IntPtr lphContext);
Where the C functional prorotype was:
MSGAPIFUNCTION MsgConnect(long lMsgAPIVersion, const char *lpcszTopic, LPOPERATOR lpOperator, MSGHANDLE *lphContext);
The lphContext is a handle, specified in C as:
typedef void *MSGHANDLE
I am trying to import this function:
MSGAPIFUNCTION MsgStartMessage(MSGHANDLE hContext, const char *lpcszActivityName, const char *lpcszMsgName, int bOriginatorVerified)
I have defined it as:
[DllImport("msg.dll",CharSet=CharSet.Ansi)]
public static extern int MsgStartMessage (ref IntPtr hContext, ref string lpcszActivityName, ref string lpcszMsgName, int bOriginatorVerified);
Note it is using the variable itself, not a pointer to it like the previous prototype. I implemented the pointer as IntPtr, not sure how to implement this - do I need to convert it to an Int?
Thanks in advance.
|
|
|
|
|
Don't use ref or out with reference types (like a string ) unless the address of a pointer is required (a pointer to a pointer to an object). So, const char * lpcszActivityName should be declared as string lpcszActivityName , for example.
FYI, you don't need to use the same parameter names. Function call execution uses a stack of parameters and pops parameters based on size. The parameter name is of no consequence when marshaling from managed to unmanaged code. Using ".NET-friendly" parameter names makes the code easier to follow.
Also, hContext should be declared as an IntPtr , not ref IntPtr . As I mentioned the last time, and IntPtr can also represent a memory address (which is a HANDLE , for which MSGHANDLE is a typedef) since it's a system-dependent size.
You really need to understand the difference between reference types (alloc'd on the heap) and value types (alloc'd on the stack; immutable), and understand the data type mapping. See Platform Invoke Data Types[^] for a mapping of basic data types.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
it's easy enough to capture a form's closing events when you hit the upper right hand corner X to do cleanup stuff, but
is it also as easy to do it with console applications? there are some things i need to do when i close the console app by
using the upper right hand X, how can i do this?
thanks!
|
|
|
|
|
When you close the command shell, it kills the process. There's really no way to trap this since the console is a different subsystem from Windows, which can send or post messages before it is shutdown.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
//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...
|
|
|
|
|