|
Hi,
I have a c++ DLL it contains a class called MyClass with methods GetName and SetName.
[DllImport("MyClass.dll")]
public static extern void GetName( string name);
This doesn't work because I need to instanciate the class.
How do I declare the class and methods within the class?
Then how do I instancial the class and call the method?
Thanks
Mike
|
|
|
|
|
No way to do this from C#. Either use MC++, or wrap your class into a COM object and the reference it from your code.
I vote pro drink
|
|
|
|
|
Hello helpers.
So far, my asteroids game is coming along nicely, and I am learning lots. Now has come the time to get c# to play some sounds - by searching around I have found this:
[DllImport("winmm.dll")]
public static extern long PlaySound(String lpszName, long hModule, long dwFlags);
This does is fact work, however, the keener amongst you will realise that code execution stops until this function returns a value. Of course, in my game, I don't want the game to freeze everytime I call this function, so, is there
a) another method for playing a sound file (I used a com object in Foxpro) and if so, can somebody tell me how to do it in C#
b) a way to call PlaySound without waiting for a return value?
Many thanks you gorgeous hamsters
J
Jason King
jason.king@profox.co.uk
Feel the love at www.profox.co.uk
|
|
|
|
|
Use ASYNC flag that has a value 0x0001.
You can use enum to include other flags:
public enum PlaySoundFlag
{
// Play synchronously (default)
Synchronously = 0x0000,
// Play asynchronously
Asynchronously = 0x0001,
// Silence (!default) if sound not found
NoDefault = 0x0002,
// pszSound points to a memory file
Memory = 0x0004,
// Loop the sound until next sndPlaySound
Loop = 0x0008,
// Don't stop any currently playing sound
NoStop = 0x0010,
// Don't wait if the driver is busy
NoWait = 0x00002000,
// Name is a registry alias
Alias = 0x00010000,
// Alias is a predefined ID
AliasId = 0x00110000,
// Name is file name
Filename = 0x00020000,
// name is resource name or atom
Resource = 0x00040004,
// Purge non-static events for task
Purge = 0x0040,
// Look for application specific association
Application = 0x0080
}
Example:
PlaySound("c:\\windows\\media\\Windows XP Startup.wav",0,PlaySoundFlag.Filename|PlaySoundFlag.Asynchronously);
Should work
Jerzy
|
|
|
|
|
Or you could use an Asynchronous method call or make your synchronous call on a different thread.
--
Peter Stephens
|
|
|
|
|
Thanks for the advice. I will try to figure out how to make an asynchronous call - I assume this would be different to the flags that Jerzy is talking about?
Thanks though, will look into it.
Jase
Jason King
jason.king@profox.co.uk
Feel the love at www.profox.co.uk
|
|
|
|
|
Asynchronous calls in .NET use delegates. There are methods on a delegate called BeginInvoke and EndInvoke. I personally have not used async calls yet, but plan to do so soon.
AFAIK .NET async calls use a threadpool behind the scenes.
--
Peter Stephens
|
|
|
|
|
Thanks alot, that's a whole lot of detail. The example clinches it. You are the man .
Cheers
J;)
Jason King
jason.king@profox.co.uk
Feel the love at www.profox.co.uk
|
|
|
|
|
Hey Jerzy,
I tried this, but for some reason, none of the flags I pass in seem to make any difference. The sound does play though. Also, for some reason, when I use the enums as per your example, the code won't compile. I have tried subsituting the enums in the example for their values, but still things don't go according to my expectation (ie, makes no difference).
Does this declaration look right for c# to you?
public static extern long PlaySound(String lpszName, long hModule, long dwFlags);
As far as I understand it, my alternatives are:
1. no sound at all
2. use a com object as per my original fox pro code
3. for every sound played, create a new thread, play the sound, kill the thread
4. do some asynch calls with beginInvoke and delegates and stuff.
Don't want to do 1., 2. seems a good thing to learn in c#, in fact as does 3 and 4.
4 however I will have to leave until I have learnt a whole lot more or until someone explains the whole delegate thing to me.
If you have any more suggestions along the lines of your original, or anything else for that matter, would love to hear 'em.
Jase
Jason King
jason.king@profox.co.uk
Feel the love at www.profox.co.uk
|
|
|
|
|
I tested this code and it's working:
....
....
....
using System.Runtime.InteropServices;
public class Form1 : System.Windows.Forms.Form
{
public enum PlaySoundFlag
{
Synchronously = 0x0000,
Asynchronously = 0x0001,
NoDefault = 0x0002,
Memory = 0x0004,
Loop = 0x0008,
NoStop = 0x0010,
NoWait = 0x00002000,
Alias = 0x00010000,
AliasId = 0x00110000,
Filename = 0x00020000,
Resource = 0x00040004,
Purge = 0x0040,
Application = 0x0080
}
[DllImport("winmm.dll")]
public static extern int PlaySound(String pszSound, int hmod,PlaySoundFlag fdwSound);
public Form1()
{
....
....
....
PlayTest();
....
....
....
}
void PlayTest()
{
PlaySound("c:\\windows\\media\\Windows XP Startup.wav", 0,
PlaySoundFlag.Filename|PlaySoundFlag.Asynchronously);
}
Jerzy
|
|
|
|
|
Profox Jase wrote:
public static extern long PlaySound(String lpszName, long hModule, long dwFlags);
Your problem is the long type. In C# this is a 64 bit int. You should use int instead.
--
Peter Stephens
|
|
|
|
|
Cool, thanks again.
J
Jason King
jason.king@profox.co.uk
Feel the love at www.profox.co.uk
|
|
|
|
|
Hi All,
I have a form which has a dropdown list on it which is being populated by an array. What I am trying to find out how to do is set a value for a option in the dropdown list so that I can query the database using a Integer and not a text field.
Example:
My Drop Down list has the following
Option 1: London
Option 2: Liverpool
Option 3: Other
If my user selects 'Liverpool' I would like to get the value '42' which is the unique ID in the database that is connected to Liverpool.
In ASP and VB you where just able to add a value to the field, can this be done in C#?
Thanks,
|
|
|
|
|
If your working in ASP.net, one of the the Add overrides lets you specify a ListItem class, which takes two strings. These map to the text of the option and the value of the option in html. Value can be retrieved from SelectedItem.Value, and the text from SelectedItem.Text.
I expect C# for apps is fairly close, but there are definately differences between the two. One difference I expect is the ability to map an object to the string instead of a second string.
// Rock
|
|
|
|
|
Using ASP I have been able to do it and that is why I expect it to be at least similar in a C# Application. There just doesn't seem to be a possible option for value which is mind boggling if they have decided to scrap that one.
Please let me know if you come across anything that explains this, also how do you mean
CLaW wrote:
One difference I expect is the ability to map an object to the string instead of a second string. ????
Thanks
|
|
|
|
|
Hi Gavin
The ComboBox in .NET receives a generic object. This is pretty powerful, but also is quite a change to the normal VB-style. All that's required is to create a simple (or more complex) class and use that to add to the ComboBox.
To get values out, just cast to your object and call your custom props. The ToString() override caught me out, but the ComboBOx uses it to show values.
private void Form1_Load(object sender, System.EventArgs e)
{
comboBox1.Items.Clear();
comboBox1.Items.Add(new cComboLoader("Bill",1));
comboBox1.Items.Add(new cComboLoader("Steve",2));
comboBox1.Items.Add(new cComboLoader("Paul",3));
comboBox1.Items.Add(new cComboLoader("Reinout",4));
}
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(((cComboLoader)comboBox1.SelectedItem).ToString());
MessageBox.Show(((cComboLoader)comboBox1.SelectedItem).myIndexVal().ToString());
}
public class cComboLoader
{
private string _Txt="";
private int _iVal=0;
public cComboLoader(string sTxt, int iVal)
{
_Txt=sTxt;
_iVal=iVal;
}
public override string ToString()
{
return _Txt;
}
public int myIndexVal()
{
return _iVal;
}
}
Does this help you?
Cheers,
Simon
X-5 452 rules.
|
|
|
|
|
Wow, just a little bit more complicated than VB ...
Well that is exactly what I was looking for but as usual it has arrived about 2 hours after I came up with another solution. I will keep it in my code bank though because it is most probably better than mine ..
What I have done is load a 2 dimensional array with the ID's and strings, then loaded the combobox with the string. When a option is chosen it loops through my Array to find the matching ID.
Hey it works
Thanks for the help though
|
|
|
|
|
You can easily combine the 2 solutions by using the temp. inline cComboLoader class in your Combo.Items.Add.
What will happen if you get 2 values with the same text, but diff. ID's?
Cheers,
Simon
X-5 452 rules.
|
|
|
|
|
There is another way, it involves using the DataSource, DisplayMember and ValueMember properties of the combo box. If I weren't late for work I'd describe exactly how to use it, but if you search the docs for DisplayMember there is an example of using it
James
Sonork ID: 100.11138 - Hasaki
"My words but a whisper -- your deafness a SHOUT.
I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
|
|
|
|
|
Hi,
I have a Socket which is binded to a local UDP endpoint. When recieving data using the Recieve() funtion, the host may timeout and my program just sits there.
Any idea on how to set a timeout ? I remember java having a method to do it which throws an exception if the timeout occours.
Any ideas people ?
Cheers
|
|
|
|
|
Hi
I am using a DLL, where one of the functions need a pointer to a C/C++ struct, here is the struct :
struct Transaction {
char card_number[21];
int card_expirymonth;
int card_expiryyear;
}
So I need to be able to declare the struct in C#, and pass a pointer from the C# "struct/class" to the DLL funcion. Of cause the sizeof should be the same. I hope it can be done
Thanks
Felix
|
|
|
|
|
When you have a struct of simple datatypes creating the struct should be fairly easy.
[StructLayout(LayoutKind.Seqential)]
public struct Transaction {
[MarshalAs(UnmanagedType.LPStr, SizeConst=21)] string card_number;
[MarshalAs(UnmanagedType.U4)] int card_expirymonth;
[MarshalAs(UnmanagedType.U4)] int card_expiryyear;
} Of course this is untested code, but I believe that is correct attributes used.
HTH,
James
Sonork ID: 100.11138 - Hasaki
"My words but a whisper -- your deafness a SHOUT.
I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
|
|
|
|
|
Hi James,
THX - I'll try that , btw. is it possible to use the sizeof() function on the struct?
Felix
|
|
|
|
|
I have the same problem
i have to pass a pointer to the DOC_INFO_1 structure via the winapi
StartDocPrinter(handle, data, *DOC_INFO_1)
DOC_INFO_1
{
LPTSTR pdocname
LPTSTR pdatatype
LPTSTR poutputfile
}
can anyone help me?
|
|
|
|
|
Oops, I think there is an error in what I suggested trying.
[StructLayout(LayoutKind.Seqential, CharSet=CharSet.Ansi)]
public struct Transaction {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)] public string card_number;
[MarshalAs(UnmanagedType.U4)] public int card_expirymonth;
[MarshalAs(UnmanagedType.U4)] public int card_expiryyear;
} What I told you the first time would have been correct if card_number were a pointer to a char* and not an array in the struct.
Sorry about that,
James
Sonork ID: 100.11138 - Hasaki
"My words but a whisper -- your deafness a SHOUT.
I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
|
|
|
|
|