|
I am having trouble with the getModuleFileNameEx api call in a VB.net application. It will not return to me the filepath of the currently active window. Any idea what i am doing wrong?
nextWnd = GetForegroundWindow()
' Gets title and path of the active window
GetWindowText(nextWnd, lpString, lpString.Capacity)
GetWindowThreadProcessId(nextWnd, procID)
modTest = GetModuleFileNameEx(procID, nullWnd, lpFileName, 500)
Thanks,
John
|
|
|
|
|
I'm not sure - I don't do VB. The following works in C# though to get the path of the executing assembly's process
StringBuilder builder = new StringBuilder(500);
uint modTest = GetModuleFileNameEx(Process.GetCurrentProcess().Handle, IntPtr.Zero, builder, 500);
Console.Write(builder.ToString());
[DllImport("psapi.dll")]
static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpFilename, int nSize);
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Ok,
After trying the code that you gave me (in a VB.net form), I get the same problem I always get:
When i output the stringbuilder object that should be holding the file path, I get 'Ÿ' followed by the title of the window. And it'll only show up for the first click of the button. So stumped.
Thank you for the help though,
~John
|
|
|
|
|
Hi,
just a hunch: maybe this call won't work as long as the window hasn't been fully initialized (e.g. when the Handle isn't available yet), so don't do this from the Forms' constructor, wait till the Load event.
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
The window is fully loaded by the time I execute this code. I have it tied to a timer that'll execute the code once every couple seconds after I hit the timer's start button.
|
|
|
|
|
I don't know why it doesn't work for you. I haven't used VB for a long time but I opened up a ne VB WinForms project and did this and it works fine - the Form's Text displays the path.
Imports System.Runtime.InteropServices
Imports System.Text
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
Text = GetPathOfThisProcessExe()
End Sub
Private Function GetPathOfThisProcessExe() As String
Dim builder As New StringBuilder(500)
GetModuleFileNameEx(Process.GetCurrentProcess().Handle, IntPtr.Zero, builder, 500)
Return builder.ToString()
End Function
<DllImport("psapi.dll")> _
Private Shared Function GetModuleFileNameEx( _
ByVal hProcess As IntPtr, _
ByVal hModule As IntPtr, _
<Out()> ByVal lpFilename As StringBuilder, _
ByVal nSize As Integer) As UInteger
End Function
End Class [Edit] Fixed code formating [/Edit]
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Sigh, this is what i HATE and LOVE about programming lol
The thing that made the entire thing work was add .handle to Process.GetCurrentProcess()
Thank you very much for your help as i had been stuck on this for some time now!
Also... what version of VB were you using out of curiosity?
Thanks,
~John
|
|
|
|
|
VB9 (Visual Studio 2008). I used to program exclusively in VB in it's various forms (including VB6 and before!) until about 2 years ago when I decided to switch to / learn C#. I found it comfortable really quickly so I've never had the desire to fire up VB since.
It was very wierd going back. I even had to google to work out how to make a sub handle an event!
Glad it helped - dusted off a few cobwebs for me
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
you can use the result of the function (UInteger) to determine if an error occurred. If there was an error, it will be zero, otherwise the length of the string (path).
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hi all
I am new to the site and sorry if I have posted on the wrong forum.
Basically I am a web developer and dealing first time with the windows application.
I need to create a custom printer via coding and add it to the list and made it available to the user whenever the user clicks on the print from the file menu.
I am using visual studio 2005 with vb.net .
Well this is the first step.
please help me. Any clues and suggestions will be highly appreciable.
Thanks
-- Palli
|
|
|
|
|
Your application should not be installing printers at all. This is either a pre-req that needs to be installed prior to running your app, or possibly even something that is installed with your application, but should not be done at run-time. There are plenty of cases where users may not be able to install printers. Since a Windows app runs as the user, it has no more permissions to the system that the user does.
|
|
|
|
|
Can I assume you're not creating a "real" printer, but a virtual one...
sort of similar to the "Send to OneNote" and "Microsoft XPS Document Writer" printers?
|
|
|
|
|
Thanks to all of you for your suggestions.
Ok..I think I should provide enough details to clear the scenario.
I need to create a custom "print to pdf" driver that when installed on a machine would appear in the print dialog of any application. When the users click on the print button, they will have an option to choose this printer. When they choose this printer, it will prompt for a location and then save files there as a PDF file.
Hope that helps.
Looking forward for your replies.
Thanks
-- Palli
|
|
|
|
|
Further research into the topic has cleared the picture more.
I need to develop a virtual printer that would be available to the user in the print list option of any application like word etc, when the user will select the virtual printer, it will ask for the location to save the file and would then save the file on the specified location as a PDF file.
Any suggestions so that I can start working would be highly appreciable.
Thanks
|
|
|
|
|
Hello everyone..
I have managed to add a virtual printer to the printer list.
And now the task is to create a PDF from the added printer.
Any suggestions regarding ?
Could I get the drivers from somewhere for the printer that creates a PDF of the document ?
Thanks
-- Palli
|
|
|
|
|
there is no direct method of doing what you want to achieve in C#
To begin with, you'll need to write your own custom printer driver for your virtual printer to use.
You will need to do a lot of api wrapping, as there is no interface in C# to help you here.
Your best bet is to start looking into the Windows Driver Development kit and see if you can find out
exactly what you need to do.
(and expect to be writing a c++ wrapper too I guess)
|
|
|
|
|
Actually i took two panels in my user control. and the functionality is i took a button on the top panel and on alternate click of the button the second panel will visible and invisible accordingly..
When we r using this user control in a new form when the button click happen and so the second panel is disabled( i mean made invisible) the controls that are present under the user control have to move up i mean they have to adjust during run time.
Can u suggest me how to do this...
Thanks and regards...
|
|
|
|
|
Just set the top and left properties of the controls. If all controls are in panels you should just have to reposition the panel.
|
|
|
|
|
dock both panels to Top. this way, when the first panel is hidden, the second one will automatically move up without any additional code. When 2 items are docked to top, the order is determined by the z-index, i.e. use BringToFront in the designer to change the order
"An eye for an eye only ends up making the whole world blind"
|
|
|
|
|
I have created a user control with a panel. I want thi panel to work as a container which can hold other controls at design time.
[Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design", typeof(IDesigner))]
I write this line before my usercontrol class. Now i am able to drag a control in to my user control but it is not acting as a child control of my user control.
For example when a drag a button on to the panel it is not visible for me but when i used to drag my user control then only it is visible. when i stop dragging my user control it is not visible and when i run this i came to know that the button is not visible for me unless the panel is made invisible during run time.. So, i came to know that the button is underlying the panel not acting as a part of the panel..
can any one help me in solving this please...
Thanks and regards
|
|
|
|
|
Hi Honey.rpk,
I've tried doing this same thing without success.
The "work-around" I found is this :
1. create a UserControl 'ucPanel
modify ucPanel so it inherits from Panel
[Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design", typeof(IDesigner))]
public partial class ucPanel : Panel
2. create a UserControl 'ucMaster : use the same Designer attribute above, but leave this a UserControl.
make sure you put references to :
using System.Drawing.Design;
using System.ComponentModel.Design;
Into both UserControls' library declarations as well as whatever else they require.
With a Form open at design-time do this :
1. drop a ucMaster on your form, and style it as you wish.
2. drop a ucPanel on your form, and move it so is "captured" by ucMaster
Now you will find you can drop controls on the ucPanel, and they will be "properly captured" at design-time which you can verify by looking at the Designer.cs code for the Form.
I realize this is not a very satisfactory solution : what you'd like to do, of course, is to design one UserControl and put elements on it like Panels (or TableLayoutPanels, or whatever other container that can "capture" controls at design-time), and then have to only drop the one UserControl on the design surface and have its sub-components also capture.
I have experimented with creating an instance of ucPanel in the constructor of the ucMaster, creating it in the load event of ucMaster, but none of these experiments work.
I'm sure there's a better way, and, hopefully, one of our "real gurus" here will comment.
best, Bill
p.s. highly recommend Matthew McDonald's book on .NET 2.0 Windows Forms and Controls[^] : Chapters 13 and 26 on design-time programming are the single best resources in this area I've found yet.
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
modified on Monday, June 29, 2009 5:27 AM
|
|
|
|
|
This[^] is what you need.
And, do not double post.
|
|
|
|
|
sorry yar i am new to this and when i posted for the 1st time i haven't found it on the screen so i tried to post it again...
|
|
|
|
|
Hi d#@nish,
Actually that article (using VB.NET) does not contain a solution to the problem the user describes.
By the way : the declaration in VB syntax in that article :
<Designer("System.Windows.Forms.Design.ParentControlDesigner, _
System.Design", GetType(IDesigner))>
Is identical in function to the attribute in C# :
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
Which the questioner has already found.
The problem described is not getting one usercontrol itself to function to "capture" other controls dragged and dropped onto it at design-time, but to deal with the question where you have one usercontrol that contains other controls that normally capture other controls (like Panels, or TableLayoutPanels).
You create a UserControl, place a Panel on it, then place the UserControl on a Form at design-time, and ONLY the UserControl will capture controls dragged on to it
Unfortunately creating another UserControl #2 (or creating a Control that derives from something like 'Panel) with the same IDesigner inheritance, and placing it at design-time on UserControl #1, and then placing UserControl #1 on a Form does NOT allow you to have UserControl #2 capture controls.
I've also tried over-riding the 'ControlAdded Event of UserControl #1 using 'OnControlAdded, which will fire at design-time, and assigning the "incoming control" to its hosted UserControl #2 : no dice there, either.
But the fact that I have tried these several experiments, does not lead me to conclude that the problem cannot be solved
best, Bill
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
modified on Monday, June 29, 2009 5:56 PM
|
|
|
|
|
Yes it is in VB.Net. If you download the source(with preview one) and run it, you will actually see the controls dropped into the user control. Being used to C#, I could not understand the code though (atleast not as quickly as I would understand C# one). Hence I just posted the link if the OP knows VB.
|
|
|
|
|