|
True, the virtual machine has its own registry, and with XP, an administrator can write to that.
|
|
|
|
|
On MSDN, I found code that is designed to change between multiple sound cards on a PC. I need to do this in .NET. I attemped to translate the VB6 code on MSDN (see link) into .NET but wind up with a PInvokeStackImbalence exception.
http://support.microsoft.com/kb/180032[^]
Here is my attempted translation:
Imports System.Runtime.InteropServices
Public Class MediaManager
Structure MCI_WAVE_SET_PARMS
Dim dwCallback As Long
Dim dwTimeFormat As Long
Dim dwAudio As Long
Dim wInput As Long
Dim wOutput As Long
Dim wFormatTag As Integer
Dim wReserved2 As Integer
Dim nChannels As Integer
Dim wReserved3 As Integer
Dim nSamplesPerSec As Long
Dim nAvgBytesPerSec As Long
Dim nBlockAlign As Integer
Dim wReserved4 As Integer
Dim wBitsPerSample As Integer
Dim wReserved5 As Integer
End Structure
Public Enum SoundFlags As UInteger
SND_SYNC = &H0
SND_ASYNC = &H1
SND_NODEFAULT = &H2
SND_MEMORY = &H4
SND_LOOP = &H8
SND_NOSTOP = &H10
SND_NOWAIT = &H2000
SND_ALIAS = &H10000
SND_ALIAS_ID = &H110000
SND_FILENAME = &H20000
SND_RESOURCE = &H40004
End Enum
Private Declare Auto Function PlaySound Lib "winmm.dll" (ByVal pszSound As String, ByVal hmod As UInteger, ByVal fdwSound As UInteger) As Boolean
Private Declare Auto Function mciSendCommand Lib "winmm.dll" Alias "mciSendCommandA" (ByVal wDeviceID As Long, ByVal uMessage As Long, ByVal dwParam1 As Long, ByRef dwParam2 As MCI_WAVE_SET_PARMS) As Long
Sub New()
Dim Parameters As MCI_WAVE_SET_PARMS
mciSendCommand(0, &H80D, &H800000, Parameters)
PlaySound("C:\thermo.wav", 0, SoundFlags.SND_FILENAME Or SoundFlags.SND_ASYNC Or SoundFlags.SND_LOOP)
End Sub
End Class
Can Anyone assist me?
Thanks,
kingneb
|
|
|
|
|
When calling native code from managed code, stack imbalances are most often caused by applying the wrong "calling convention" (which says who, caller or callee, is responsible for cleaning up the stack). You will need to apply an explicit DllImport directive and specify the correct calling convention, most likely StdCall .
You will find an example here[^].
I wrote a little article[^] on P/Invoke, unfortunately it isn't finished yet, so it doesn't have the details for VB.NET; you might find it useful anyway.
|
|
|
|
|
OK, I specified the standard calling convention, but no cigar? I think it is something else unless I am still not using the right calling convention.
Imports System.Runtime.InteropServices
Public Class MediaManager
Structure MCI_WAVE_SET_PARMS
Dim dwCallback As Long
Dim dwTimeFormat As Long
Dim dwAudio As Long
Dim wInput As Long
Dim wOutput As Long
Dim wFormatTag As Integer
Dim wReserved2 As Integer
Dim nChannels As Integer
Dim wReserved3 As Integer
Dim nSamplesPerSec As Long
Dim nAvgBytesPerSec As Long
Dim nBlockAlign As Integer
Dim wReserved4 As Integer
Dim wBitsPerSample As Integer
Dim wReserved5 As Integer
End Structure
Public Enum SoundFlags As UInteger
SND_SYNC = &H0
SND_ASYNC = &H1
SND_NODEFAULT = &H2
SND_MEMORY = &H4
SND_LOOP = &H8
SND_NOSTOP = &H10
SND_NOWAIT = &H2000
SND_ALIAS = &H10000
SND_ALIAS_ID = &H110000
SND_FILENAME = &H20000
SND_RESOURCE = &H40004
End Enum
Private Declare Auto Function PlaySound Lib "winmm.dll" (ByVal pszSound As String, ByVal hmod As UInteger, ByVal fdwSound As UInteger) As Boolean
<DllImport("winmm.dll", CallingConvention:=CallingConvention.StdCall)>
Private Shared Function mciSendCommand(ByVal wDeviceID As Long, ByVal uMessage As Long, ByVal dwParam1 As Long, ByRef dwParam2 As MCI_WAVE_SET_PARMS) As Long
End Function
Sub New()
Dim Parameters As MCI_WAVE_SET_PARMS
mciSendCommand(0, &H80D, &H800000, Parameters)
PlaySound("C:\thermo.wav", 0, SoundFlags.SND_FILENAME Or SoundFlags.SND_ASYNC Or SoundFlags.SND_LOOP)
End Sub
End Class
Thanks,
kingneb
|
|
|
|
|
Are you still getting StackImbalance exceptions, or something else?
Using P/Invoke is very similar to programming assembly code, it requires great meticulousness.
In this short time, you did not read the MS link I provided and my article. If you had, you would know that data types differ in managed and native worlds, so your long s very likely are wrong too. And there may be more. Don't expect this to work in a matter of minutes, start with a simple function and get that working, then build from there.
Take your time, experiment, learn the technology. And when you run into real trouble, ask specific questions providing detailed and accurate observations. "No cigar" isn't helpful.
|
|
|
|
|
On top of what Luc said, if you're converting VB6 code to VB.NET the biggest trip is using the wrong data types:
VB6 type VB.NET type
--------- = ------------
Byte Byte 8-bit unsigned integer
Integer Short 16-bit signed integer
Long Integer 32-bit signed integer
Single Single 32-bit floating point
Double Double 64-bit floating point
Currency Decimal (not a direct conversion)
When the function you're calling expects a certain size value, using the wrong type will most assuredly screw up the stack.
Also, you may want to bookmark P/Ivoke.net. It has the correct signatures for most Win32 functions for C# and VB.NET. For example, PlaySound[^] and mciSendString[^], as well as tons more.
|
|
|
|
|
In PlaySound, the second parameter is an IntPtr, the third an Int32.
In mciSendCommand, the first parameter is an IntPtr. Also the last parameter is likely an IntPtr, i.e. you'll have to use Marshal.Alloc and Marshal.StructureToPtr .
|
|
|
|
|
Any luck yet? I have received this error when my parameters didn't quite map between VB.Net types and unmanaged types. If you still need help, I can take a look and see if I can get a working PlaySound sample.
|
|
|
|
|
If I change the datatypes in the method declaration, and change the way its called by using Marshal to copy the values back and forth from unmanaged. I am getting a result of 257 which means 'Invalid DeviceID' which is expected since we passed it 0 as a deviceID.
Example:
'Used to change between different sound cards.
Private Declare Auto Function mciSendCommand Lib "winmm.dll" Alias "mciSendCommandA" (ByVal wDeviceID As UInteger, ByVal uMessage As UInteger, ByVal dwFlags As IntPtr, ByRef dwParam2 As IntPtr) As Integer
Public Sub TestMciSendCommand()
Dim Parameters As MCI_WAVE_SET_PARMS
Dim fwdCmd, dwParam As IntPtr
Dim mciResult As Integer
Dim deviceID As UInteger
Try
dwParam = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(MCI_WAVE_SET_PARMS)))
Marshal.StructureToPtr(Parameters, dwParam, False)
fwdCmd = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(Integer)))
Marshal.WriteInt32(fwdCmd, &H800000)
deviceID = 0 ' TODO: Get the actual deviceID
' results in error # 257 - MCIERR_INVALID_DEVICE_ID
mciResult = mciSendCommand(deviceID, &H80D, fwdCmd, dwParam)
Parameters = CType(Marshal.PtrToStructure(dwParam, GetType(MCI_WAVE_SET_PARMS)), MCI_WAVE_SET_PARMS)
Catch ex As Exception
Debug.WriteLine(String.Format("mciSendCommand Failed with message: {0}", ex.Message))
Debug.WriteLine(ex)
Finally
' Clean up unmanaged memory allocations!
Marshal.FreeHGlobal(fwdCmd)
Marshal.FreeHGlobal(dwParam)
End Try
End Sub
Error Codes Reference[^]
Hopefully all that remains is to find the proper deviceID. Best of luck to you.
|
|
|
|
|
System.Web.DataVisualization.dll i need this file so if you have this file then please upload .
Thank you in advance.
|
|
|
|
|
The file is part of a set, I suggest you install the entire package.
MS Chart Controls[^]
Next thing to do is to pick up a sheet of paper, and to title it "documentation". Jot down all the files that you're referencing, why they were needed, and where to get 'em.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Hello !
i'm working on a vb.net 2010 /SQL server 2008 / Entity Framework
i have a bound datagridview with 3 columns , and inside datagrid i have an unbound combobox column that have some numbers as items (1,2,3).
if i try to select a value from unbound combobox , after leaving the cell the value on combobox is back to default value.
what can i do ?
modified 16-Jul-12 21:26pm.
|
|
|
|
|
Is this an Asp.Net project? One idea would be to make it a bound column and create a layer of indirection for the data source by using a custom class that takes into account the extra column. Another would be to check out the viewstate, maybe? That's assuming that it is an ASP.net project.
|
|
|
|
|
HI I am not expert in visual, but i try to make a program to search text in multi excel sheets. My setup is like this
I have one textbox1(to write the word i want to search for)
one search buttom
Listbox1(To add files strings for the search)Working good
buttom for clear all,clear selected,and add also working here
Listbox2(To see the result when i press search.) Not working
buttom for clear all,clear selected,and add also working here(working)
My problem is that i have searched all over to find the code for the search buttom.
I have tried many exsamples, but it always dont work.
Function is that i will add files to Listbox1(one or more)
then i textbox1 i will type the search word.
In the search buttom i press search
And then in Listbox2, The result will come out in strings(would like if i can open these string also, or see th formula from the excel sheet)
Would highly appriciate some, who have solved this or have a working solution.
I am not expert, so a full code would be appriciated.
Thanks in advance
Sincerely
abjac .
|
|
|
|
|
abjac@ wrote: Listbox2(To see the result when i press search.) Not working You need to explain what "not working" means if you expect people to be able to make useful suggestions to help you.
|
|
|
|
|
Sorry that was maybe the wrong expression. I made this setup and the search bottum(button1), when i press it it always comes up with and error. But the fact is that the code comes up with this error.
Could not find a part of the path 'C:\Documents and Settings\Alancecille\Desktop\listbox1.text'.
DirectoryNotFoundException was unhandled
But this is the path i add in listbox1 with out any problems. So i dont know why it can not find it.And also i dont know if i can use this code. The idea is to add the files(xls) i want to search in in Listbox1 and then then type the search word in textbox1, and when i press search, it shall find all strings(or formula) in the excel sheet, which have this word included. Please have a look at below code and come with some suggestions.
Thanks in advance
abjac
This is my code and as you can see the code for bottun1 is the one which cause the error, but also i think this maybe is not correct.
Imports System.IO
Public Class Form1
Dim streamer As IO.StreamReader
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim open As New OpenFileDialog
Try
open.Title = "Open Excel Sheets"
open.FileName = "xls"
open.Multiselect = True
open.Filter = "xls files |*.xls"
If open.ShowDialog = Windows.Forms.DialogResult.OK Then
For Each file As String In open.FileNames
ListBox1.Items.Add(file)
file = False
Next
End If
Catch ex As Exception
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
ListBox1.Items.Clear()
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
ListBox2.Items.Clear()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
ListBox2.Items.Remove(ListBox2.SelectedItem)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim filenames = My.Computer.FileSystem.FindInFiles("listbox1.text", "textbox1.text", True, FileIO.SearchOption.SearchAllSubDirectories)
For Each filename As String In filenames
ListBox2.Show()
Next
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Dim SaveFile As New SaveFileDialog()
SaveFile.Filter = "Multi search in Excel |*.xls"
SaveFile.FileName = "xls"
SaveFile.ShowDialog()
Dim WriteFile As New System.IO.StreamWriter(SaveFile.FileName)
Dim Count As Integer = ListBox1.Items.Count - 1
For i As Integer = 0 To Count
WriteFile.Write(ListBox2.Items.Item(i) & ";") 'the “;” is used to separate the items
Next
WriteFile.Close()
End Sub
End Class
modified 16-Jul-12 16:44pm.
|
|
|
|
|
My.Computer.FileSystem.FindInFiles(directory:= As String,containsText:= As string)
You are giving a constant text
Dim filenames = My.Computer.FileSystem.FindInFiles("listbox1.text", "textbox1.text", True, FileIO.SearchOption.SearchAllSubDirectories)
Your dir is something like C:\dir1\dir2
Your text must be without quotes
Jan
|
|
|
|
|
The first thing to do whenever a method throws an exception or returns an error is read the documentation[^] to check that your parameters are correct. In the above code you have
Dim filenames = My.Computer.FileSystem.FindInFiles("listbox1.text", "textbox1.text", True, FileIO.SearchOption.SearchAllSubDirectories)
and as you can see, parameter 1 is not a directory path so it will fail.
|
|
|
|
|
Hi My idea was that it should read,,, "Listbox1.text",,, and the path i add there and the same with the search word
read in this "textbox1.text" and the word i type there
I dont know how to do this, but i think if i put example the directory path directly in here and the search word after it would work. But how do i make this so it will search in the directory, what i add in Listbox1.text. ??
And search there for the word i type in Textbox1.text??
So i am sure i miss some code for make this to work. If any suggestions, it would be nice.
abjac
|
|
|
|
|
You just need to replace the strings in your method call with proper parameters which you initialise from the listbox [^] and the textbox [^]. Something like:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim litem As ListBoxItem
Dim dir As String
litem = listbox1.SelectedItem
dir = litem.Content.ToString
Dim filenames = My.Computer.FileSystem.FindInFiles(dir, textbox1.text, True, FileIO.SearchOption.SearchAllSubDirectories)
|
|
|
|
|
HI Thats great i will try when i come home tonight, still working here. But i will try and let you know if its working. Thanks for now abjac
|
|
|
|
|
HI I tried this code but its not working, i have tried different ways to avoid the error but wit no luck
The error is in this line "Dim Item As ListBoxItem"
Error 1 Type 'ListBoxItem' is not defined.
Try also to have a look to below code where i want to see the the result in Listbox2. Is this one ok you think?
Please have a look thanks
abjac
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Item As ListBoxItem
Dim dir As String
Item = ListBox1.SelectedItem
dir = Item.Content.ToString
Dim filenames = My.Computer.FileSystem.FindInFiles(dir, TextBox1.Text, True, FileIO.SearchOption.SearchAllSubDirectories)
For Each filename As String In filenames
ListBox2.Show()
Next
End Sub
|
|
|
|
|
HI I tried it and it didnt work get this error
'ListBoxItem' is not defined.
But now i am also not clear of if this is correct. I think this is for search in a folder but i want to search in an excel sheet after the word and display the info in Listbox2 where the word is found in the excel file
Any suggestions?
abjac
Dim Item As ListBoxItem
Dim dir As String
item = ListBox1.SelectedItem
dir = item.Content.ToString
Dim filenames = My.Computer.FileSystem.FindInFiles(dir, TextBox1.Text, True, FileIO.SearchOption.SearchAllSubDirectories)
For Each filename As String In filenames
ListBox2.Show()
Next
|
|
|
|
|
abjac@ wrote: 'ListBoxItem' is not defined.
Check the MSDN documentation[^].
abjac@ wrote: I think this is for search in a folder but i want to search in an excel sheet
Then I wonder why you are using FindInFiles .
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Well i am not expert, but thought FindInFIles was the solution, I want to find a word in a excel file. But please if you have any other solution, would be great to hear about
Thanks in advance
abjac
|
|
|
|
|