|
Thanks Daniel,
Two more comments,
1.
"No, that's not how method group is used in the spec (at least how I remember it)." -- I am losing the context.
You mean my understanding that "method group is a set of methods with the same signature" is wrong?
2.
"An expression like "Console.WriteLine" returns a method group." -- Console.WriteLine returns void. Why do you say it returns method group?
regards,
George
|
|
|
|
|
1.
George_George wrote: You mean my understanding that "method group is a set of methods with the same signature" is wrong?
Yes. A method group is a set of methods with different signatures. There are 19 different "Console.WriteLine" methods (19 overloads with different parameters).
2.
"Console.WriteLine" is an expression that returns a method group.
You can do one of two things with a method group:
a) Invoke a method from it: "Console.WriteLine()" - this chooses one of the overloads compatible with the parameters you are passing, and invokes it.
b) Assign it to a variable of a delegate type "Action a = Console.WriteLine" - this chooses one of the overloads compatible with the delegate signature, and creates a delegate from it.
So the return type of "Console.WriteLine" is a method-group. The return type of "Console.WriteLine()" is void.
You cannot declare "method-group" variables, only variables of delegate types that store only a single method from the group.
|
|
|
|
|
Thanks Daniel,
1.
Now I have understood why my previous understanding -- method group is a set of methods with the same signature. But what is the exact definition of a method group? I found it is too flexible to understand, e.g. how could I know whether an expression like Console.WriteLine returns a method group, any easy to understand and remember rules?.
2.
Console.WriteLine could have 19 overloads, wow! But for delegate we still need to specify the parameter type/number and return value type, how could you use a single delegate variable/type to point to different methods with different parameter type/number and return value type -- like Console.WriteLine?
regards,
George
|
|
|
|
|
Hello all.
Does anybody know how to query the available mail servers active on a mchine prior to calling the send method on the MailMessage class. i.e. There could be a SMTP or exchange server on the network.
Cheers.
|
|
|
|
|
Matt Fishbeck wrote: . There could be a SMTP or exchange server on the network.
SMTP is a protocol. Exchange is a product. Exchange can handle SMTP (if enabled).
|
|
|
|
|
Is there a call to query the STMP server to be used as a parameter to be passed into the constructor of SmtpClient?
|
|
|
|
|
If not supplied, the SmtpClient class will lookup the info in the app.config file. If not present there, it will use the defaults as defined in the machine.config file.
|
|
|
|
|
I just noticed the ServicePoint property, that could be helpful, never used it
|
|
|
|
|
We are using an exchange server. Do not know if it supports STMP but without supplying a server it throw an exception Will see what I can find out
|
|
|
|
|
Hello!
I'm starting to use C#, and I feel lost right now.
I've got a "somestruct.h" file with a C++ struct:
struct SOMESTRUCT
{
TCHAR Field1[100];
int Field2;
double Field3;
};
And, in a C++ class, I have a function that returns a SOMESTRUCT:
SOMESTRUCT someFunction();
Now I have a C# app which will call the C++ function. I have the C++ code in a DLL and I try to import the DLL so I can call that function:
[DllImport("myDLL.dll", CharSet = CharSet.Auto)]
static extern SOMESTRUCT someFunction();
Problem is: my C# app doesn't know the SOMESTRUCT struct. I can't import "somestruct.h" in C# and I really don't know how could I use that function.
I think maybe I could make a managed version of the struct SOMESTRUCT, but I don't know how to transform a fixed-size char array (Field1). If I transform it to a String it doesn't work properly.
I have also read that I could make a wrapper of the struct to get it work from C#, but I just cannot find an easy-to-understand, newbie-suitable, complete example of how to do it.
Please, could somebody help me?
|
|
|
|
|
verence333 wrote: Please, could somebody help me?
Learn C# first, then move onto more advanced topics like platform invoke (calling a native dll).
|
|
|
|
|
Hi, leppie.
Well, I'm trying to learn C# already. But I'm not learning it because I want, but because I'm required to get an application working. That application uses some C++ DLL's, and I *must* use them. Also, I don't have all the time in the world to learn how to do it, it should be done in a limited time, so I must learn advanced topics already.
I think I have no problem with C# language, it's very easy to understand and use, and the only things that trouble me are those advanced topics. I already know how to invoke functions from native DLL's, but only functions that return a standard type (int, long, etc.), and I can't find info about how to invoke functions that return a struct.
Thanks.
|
|
|
|
|
I suggest you lookup on Marshalling structures, that should give you the answer you are looking for, whether it will be understood is another story.
Good luck
|
|
|
|
|
OK, thank you. I'll try to find that. I've already learnt something about marshalling, like for example:
(SOMESTRUCT) aaa = (SOMESTRUCT)Marshal.PtrToStructure(param, typeof(SOMESTRUCT));
And it works if SOMESTRUCT is defined in the C# app. But, what if it isn't, and is a struct that is in a C++ DLL? I just can't cast to a SOMESTRUCT.
I hope I can find that info searching for what you suggest. Anyway, if somebody has more suggestions, I will be glad to read them.
Thank you!
|
|
|
|
|
verence333 wrote: And it works if SOMESTRUCT is defined in the C# app. But, what if it isn't, and is a struct that is in a C++ DLL? I just can't cast to a SOMESTRUCT.
No you cant. You have to define it in C# somewhere so the magic can work
verence333 wrote: (SOMESTRUCT) aaa = (SOMESTRUCT)Marshal.PtrToStructure(param, typeof(SOMESTRUCT));
You do not need it that explicity most of the time. Eg
extern static void Foo(ref SOMESTRUCT s);
See! All magic
|
|
|
|
|
Interesting, thanks again.
I've come up to this url:
http://msdn.microsoft.com/en-us/library/ms384329(VS.71).aspx[^]
There, they explain how to make it, but the examples don't work. For example, the following line:
MCITY(String* name,int x,int y)
and this other:
String* name;
Throw an error: "Cannot take the address of, get the size of, or declare a pointer to a managed type ('string')". So I still don't know how to marshall a char pointer or char array. :-/
Anyway, it's a start.
|
|
|
|
|
There are several attributes in the System.Runtime.InteropServices namespace that will be useful here. You could try something like this:
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
struct SOMESTRUCT
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst:=100)]
string Field1;
int Field2;
double Field3;
}
I don't know if that should be SizeConst:=100 or just SizeConst=100; I just copied it from the VB editor. At the very least, that should point you in the right direction.
|
|
|
|
|
Wow! Thank you so much, Gideon! Yes, it really helped a lot! (BTW, it's just SizeConst=100)
I could never think that finding this answer was so difficult. I've been searching and asking for several days with no results until now.
|
|
|
|
|
Hi!
Gideon, yesterday I used your code to solve the problem, but I needed to add just a little thing (the charset in the StructLayout should be equivalent to the one used in the C++ struct; I just used "Auto" and it works). Just in case somebody has the same question, I'll write it here:
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
struct SOMESTRUCT
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
string Field1;
int Field2;
double Field3;
}
Thank you very much.
|
|
|
|
|
I had a similar problem, and I was able to avoid the complexities of marshalling by defining the same structure in both C# and C++.
A "char" is C# is NOT the same as a "char" in C++. C# uses unicode chars, which are usually longer than one byte. But a C# "byte" IS the same as a "char" in C++.
After you have what appear to be equivalent structures in both languages, write a test program that sets values in one, and see if you can pass it to the other unchanged. You can also use the sizeof () operator to see if the structure is the same size in both languages. (C# allows sizeof () to be used in unsafe mode.)
Ints and doubles are the same in both languages.
|
|
|
|
|
Thank you, Alan. I tried to write the equivalent struct, but my problem was when translating a char[100]. C# does not allow me to make structures with fields that have been initialized to a size. So, if I write this inside a struct in C#:
public byte[] bytes = new byte[100];
I have the following error: "cannot have instance field initializers in structs".
Thanks for the rest of your message, I'll test my program as you suggest. In fact, I tried to do this before, but I couldn't use sizeof() and forgot it; didn't knew I could use it in unsafe mode.
|
|
|
|
|
|
Awesome! Thank you very much, Alan! This makes my job MUCH easier.
|
|
|
|
|
Hi.
Can anyone describe that clearly ?
Thank you.
|
|
|
|
|
An assembly is either an EXE or a DLL.
|
|
|
|