|
Im facing "attepted to divide by zero" exception in my audio steganography code made in c#.
I think the error is near the line:
// The key must be repeated, until every bit of the message has a key byte.
double countKeyCopies = messageLengthBits / keyLength;
please provide the solution.
Krrish
|
|
|
|
|
Well, based on the code you have provided:
if (keyLength != 0)
{
double countKeyCopies = messageLengthBits / keyLength;
... Is about the only advice we can give...
If I was you, I'd be looking at why keyLength is zero, not trying to work round the fact. And that's elsewhere in your code.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
hey OriginalGriff,
Thanks for the solution
After doing that change, im facing new problem....
if (m_Stream.Position >= m_Stream.Length)
{
Console.Write("before ex");
throw new Exception("Invalid file format");
}
here this exception is fired, why position of stream is exceeding the length?
Please help...
(and i will now onwards use the key with nonzero length )
Krrish
|
|
|
|
|
This may come as a surprise to you, but I can't see your screen, access your HDD, or read your mind...
I would suspect it's related to your previous problem, and that you are just "papering over the cracks" in something that is fundamentally flawed elsewhere - what you are seeing here are symptoms, but they aren't necessarily the problem.
It's similar to you driving down the road and noticing a lot of abnormal road noise. So you turn the radio up. Then you notice that it's really difficult to steer, so you decide to use long straight roads only.
The problem is that one of your tires was flat and has started to fall apart completely. And if you don't stop and fix it you are going to have even more problems in a minute!
Stop trying to fix symptoms, and look for the source of the problem!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Ya bro.... i understand what u r telling, bt im new at c# coding so facing problems.
|
|
|
|
|
Please, don't use textspeak here - it can really annoy people, and that reduces your chances of getting a helpful answer.
I can't help you fix your code - I don't have a clue what you have written, and I couldn't run it if I did as I haven't got your data either - so it's going to be up to you.
Start with the "original" code - I'm pretty sure you didn't write it all - and look ate what is happening with the data there. Then look at what the code you added does to that data: that should give you clues as to what you are doing wrong.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Because you read past the end of the file?
Without seeing the bigger code that this is a part of it's impossible to tell you what you did wrong.
|
|
|
|
|
Have you attempted to retrieve anything from the stream yet? If not, add the following beforehand:
m_Stream.Position = 0;
|
|
|
|
|
I have an application that calls the run prompt, but the OS puts the run prompt in a strange location. I can't seem to identify the hWnd of the window so that I can use pin invoke commands to move it?
Has anyone ever been able to identify the run prompt window and send a command to the OS to move it?
64bit
c:\Windows\SysWOW64\rundll32.exe shell32.dll,#61
32bit
c:\Windows\System32\rundll32.exe shell32.dll,#61
|
|
|
|
|
Ummm....why?
It's a Window that belongs to Explorer.
Also, anything you do in the Run window can be done with Process.Start(). What's with the obsession with the Run window?
|
|
|
|
|
Explorer is putting the prompt in an odd location and I would like to be able to control that.
I've created a GUI toolbar for server 2012, which is why process.start isn't an option in this case as a replacement for a GUI prompt.
|
|
|
|
|
turbosupramk3 wrote: xplorer is putting the prompt in an odd location and I would like to be able to control that.
There is a problem in Explorer that is causing it. I have no idea what the cause of the problem is though. What I've done to "fix" it is to login at the console as a new user that has never logged in on the machine before. Hit Win-R to bring up the window and it always shows up in the proper place. I then logout and back in again as the user affected, and it's fixed.
I've only seen this happen twice - ever.
You're trying to code around a problem, not fixing the problem.
turbosupramk3 wrote: I've created a GUI toolbar for server 2012, which is why process.start isn't an option
That makes no sense. If you're replacing something (you don't specify what) then that makes Process.Start even more effective as a solution.
|
|
|
|
|
It makes perfect sense. I've created a GUI tool bar since server 2012's version sucks. Part of that is familiarity, and one of the buttons I have calls the run prompt via the code I posted in my first post, which does use process.start behind the scenes to make that call. Click on the button for that and up the old familiar run prompt comes. I do not know why Windows places it on screen where it does, but I'd like to move it dependent on where a user puts the tool bar at on screen and not have that static odd ball location.
Now, do you know how I can move this Explorer shell window? Spy++ can see a window handle for it and I can drag it around the screen manually ... usually when I can do something manually, I can do it programmatically.
|
|
|
|
|
Get the name of the window, verify it in Spy++, and you can find it with this[^].
Once you have the handle you can set it's position with SetWindowPos[^].
|
|
|
|
|
Thanks Dave. I tried something like this by iterating through the processes, I will try with that example and report back tomorrow.
|
|
|
|
|
I believe the MainWindowHandle value of 0 is what is making this tough for me. Here is what I am seeing and this is what I had encountered before, no way to rectify what I'm seeing in spy++ to what I have when I enumerate through the process information. I still think the limitation is me though and not Windows or else how would spy++ have the alphanumeric value in the handle box?
http://s26.postimg.org/jlajsrx49/run_Prompt_Step_Through1.png[^]
|
|
|
|
|
You haven't said how you are trying to get the handle of the Run window but it is possible with the native function EnumWindows.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class ProcessWindowFinder {
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumWindows(EnumWindowsProc callbackMethod, IntPtr state);
[DllImport("user32.dll")]
static extern UInt32 GetWindowThreadProcessId(
[In] IntPtr hwnd,
out UInt32 processId);
delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr state);
public void FindWindows(Process selectedProcess) {
EnumWindows(SelectWindowsByPID, (IntPtr)selectedProcess.Id);
}
private bool SelectWindowsByPID(IntPtr hwnd, IntPtr state) {
Int32 pid = state.ToInt32();
Int32 windowPid = GetWindowPID(hwnd);
if (pid == windowPid) {
}
return true;
}
private Int32 GetWindowPID(IntPtr hwnd) {
UInt32 PID;
GetWindowThreadProcessId(hwnd, out PID);
return unchecked((Int32)PID);
}
}
The callback function SelectWindowsByPID filters the handles down to those belonging to the selected process, in your case rundll32. You could filter further by window text (native GetWindowText) and visibility (native IsWindowVisible) but I think that final application specific filtering should really be done outside the ProcessWindowFinder class which is why I suggest raising an event.
Alan.
|
|
|
|
|
Hi Alan,
Thanks for the reply. I'm trying to figure which function to plug information into, to test the code you posted.
If it is EnumWindowsProc(IntPtr hWnd, IntPtr state);, I don't have the hWnd? Can you lead me a long a little more please?
|
|
|
|
|
For some reason, you seem obsessed with passing a window handle into a function to find another window handle. You just need the Caption of the window:
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
private void button1_Click(object sender, EventArgs e)
{
IntPtr runHandle = FindWindow((string)null, "Run");
Debug.WriteLine(string.Format("Run window handle: {0}", runHandle.ToString()));
}
|
|
|
|
|
I'm not obsessed with anything but learning. Here is what the IDE told me when I tried that code.
"Unable to find an entry point named 'FindWindow' in DLL 'user32.dll'."
|
|
|
|
|
What does your copy of the code look like? FindWindow has been in user32.dll since the beginning of Windows.
|
|
|
|
|
I closed and reopened the solution and it worked? Thank you for this, it was such a simple solution, I believe I tried something similar before originally posting but if I remember correctly I would always get a handle of 0 ... it turns out there is a slight delay/lag so before FindWindow will work on a spawned process, as the process has to load. I was not able to figure that out before, but was after you posted your code. Thank you.
@Alan, if you read this, I would still like to learn how your class ties together, and how it could be tweaked.
private void btnTest_Click(object sender, EventArgs e)
{
Process p = Process.Start(@"c:\Windows\System32\rundll32.exe", "shell32.dll,#61");
int timeOutCounter = 0;
IntPtr runHandle = IntPtr.Zero;
do
{
runHandle = FindWindow((string)null, "Run");
Thread.Sleep(100);
timeOutCounter = timeOutCounter + 1;
} while ((runHandle == IntPtr.Zero) && (timeOutCounter < 30));
if (runHandle != IntPtr.Zero)
{
SetWindowPos(runHandle, 0, 800, 800, 425, 235, 0X4 | 0x0040);
}
}
|
|
|
|
|
OK,
The only public method in the class is FindWindows. It calls the native EnumWindows method which enumerates all top level window handles and for every window handle found a callback method is invoked. The method, which has the signature bool EnumWindowsProc(IntPtr hwnd, IntPtr state), is SelectWindowsByPID.
So some code that starts the process you are interested in and then enumerates the window handles,
Process p = Process.Start(@"c:\Windows\System32\rundll32.exe", "shell32.dll,#61");
ProcessWindowFinder finder = new ProcessWindowFinder();
finder.FindWindows(p);
On my system 267 window handles were found and SelectWindowsByPID was invoked once for each of those handles. The filtering by process ID showed that only 3 were associated with the rundll32 process.
To determine which of those 3 handles identifies the "Run" window use the native GetWindowText function. There is an example c# method on pinvoke.net http://www.pinvoke.net/default.aspx/user32/GetWindowText.html[^]. For testing insert your code inside the if block where I inserted the comment // Do something here .
Alan.
|
|
|
|
|
Hi Alan,
This is quite humbling. I honestly cannot figure out how to modify your class, ashamedly.
What I am trying to do is to get the class to return a list of hwnds and pids and then I was going to use GetWindowText to narrow down the proper hwnd while parsing through that list, as I am getting 4 or 5 handles found. This may be the wrong way to go about this, and this might be why I am having such trouble with it.
Using a Messagebox.Show in place of the // Do something I was able identify the proper pid and handle associated with it, but embarrassingly, I cannot figure out how to create an array of some sort, add each handle found and then return it to my calling function so that I can then parse through the returned data. I cannot figure out how this part of the code works
EnumWindows(SelectWindowsByPID, (IntPtr)selectedProcess.Id);
when SelectWindowsByPID requires two parameters by delclaration
IntPtr hwnd, IntPtr state and I see that it somehow interacts with a static extern, which I have never used before. I apologize for my ignorance, but I'm self taught and apparently have discovered a large hole in my learning.
|
|
|
|
|
I see you've got Dave's FindWindow suggestion working and it's certainly a lot simpler than my method.
As you asked about EnumWindows here goes, although you may have to do some reading yourself, as getting callback functions to work reliably requires some advanced techniques.
First part of the answer on how EnumWindows works
EnumWindows(SelectWindowsByPID, (IntPtr)selectedProcess.Id);
The .NET program calls EnumWindows and supplies a method that will be called by Windows for every handle that is found. This is the callback method, the c# signature of which is fixed as
delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr state);
Windows sets the first parameter to the window handle it has found. The second parameter is a copy of the second argument supplied to EnumWindows, which provides a way for the programmer to pass information into the callback. I've used it to pass in the process Id.
Fully operational examples have additional complexity as it is essential to stop the .NET garbage collector moving any object that is referenced by the callback function. Rather than modify the original code I'll show you a complete class that gives a list of window handles.
using System;
using System.Runtime.InteropServices;
public class ProcessWindowList2 : System.Collections.Generic.List<IntPtr> {
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumWindows(EnumWindowsProc callbackMethod, IntPtr state);
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr state);
[DllImport("user32.dll")]
private static extern UInt32 GetWindowThreadProcessId(
[In] IntPtr hwnd,
out UInt32 processId);
private readonly Int32 pid;
private static Int32 GetWindowPID(IntPtr hwnd) {
UInt32 PID;
GetWindowThreadProcessId(hwnd, out PID);
return unchecked((Int32)PID);
}
private static Boolean SelectWindows(IntPtr hwnd, IntPtr state) {
GCHandle gch = GCHandle.FromIntPtr(state);
ProcessWindowList2 list = (ProcessWindowList2)gch.Target;
Int32 windowPid = GetWindowPID(hwnd);
if (list.pid == windowPid) {
list.Add(hwnd);
}
return true;
}
public ProcessWindowList2(Int32 processId) {
this.pid = processId;
EnumWindowsProc callback = new EnumWindowsProc(SelectWindows);
GCHandle gch = GCHandle.Alloc(this);
try {
EnumWindows(callback, GCHandle.ToIntPtr(gch));
} finally {
gch.Free();
}
}
}
This is a modified version of published code http://msdn.microsoft.com/en-us/magazine/bb985713.aspx[^]. It's derived from List<IntPtr> but has a special constructor that populates the list with handles. The code makes use of GCHandle.Alloc(this) to inform the garbage collector that objects of this class must not be moved until GCHandle.Free is called. This is essential for the safe operation of the callback.
Using the class is very simple
Process p = Process.Start(@"c:\Windows\System32\rundll32.exe", "shell32.dll,#61");
ProcessWindowList2 hwndList = new ProcessWindowList2(p.Id);
foreach (IntPtr hwnd in hwndList) {
}
Have fun!
Alan.
|
|
|
|
|