Rune Bune’s Blog

Games, Gadgets, Code & Tech

Archive for the ‘Code’ Category

Programming code

Use a .NET Windows Form as a Console application also

Posted by runebune on May 10, 2010


Some times I want to command line my windows form applications and get the output from it direct to the command line. This is nice to have when I want to batch or run the program with-in another program.

I use it if I have some methods I want to use in both a Windows Form and command line application and I don’t want to write separate applications or wrappers.

I don’t want to make a Console application and start a Windows Form, because the console windows will always be visible. I want a Windows Form application to also work as a Console application with arguments and output.

To do this I had to use the kernel32.dll as shown below:

[DllImport(“kernel32.dll”, SetLastError = true)]
private static extern bool AttachConsole(int processId);

[ DllImport(“kernel32.dll”, SetLastError = true)]
private static extern bool ReleaseConsole();

DllImport namespace is System.Runtime.InteropServices

AttachConsole(int processID) takes the console process ID to which it will output to, -1 is current.
ReleaseConsole() releases the attached console.
Example:
[STAThread]
static void Main(string[] args)

{
  try
  {
    if (args.Length == 0)
    {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new WindowsTestForm(args));
    }
    else
    {
      AttachConsole(-1);
      WindowsTestForm testForm= new WindowsTestForm (args);
    }
  }
  catch (Exception)
  {

  }
  finally
  {
    ReleaseConsole();
  }
}

#region Use Console in Windows Forms

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AttachConsole(int dwProcessId);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool ReleaseConsole();

#endregion

Posted in .NET, Code, Tips & Tricks | Tagged: , | Leave a Comment »

drPodder Media Importer 0.3.1.0 released

Posted by runebune on May 5, 2010


Change log :0.3.1.0 (Beta)

-Fix database with Not existing album art on device
-Detect album art files on restore and clear in db if not existing (eg. restore on full erased USB partition)
-Clear downloaded flag if media files (episodes) are missing on device
-Clear downloaded and new flag if media files (episodes) are missing on device. Can be used if the restore (from eg. wobOS Internals Save/Restore
-Downloading files flaged as downloaded but does not exist on device if drPodder is capable of deleting (does not work if the download tickets in downloadhistory.db in webOS are erased eg. data erease, firmware flash I think).

download

Posted in .NET, Code, Info, Mobile | Tagged: , , , | Leave a Comment »

drPodder media importer alpha released

Posted by runebune on March 15, 2010


Now I just released an alpha version of the drPodder media importer software

http://helioxware.wordpress.com/2010/03/15/drpodder-media-importer-alpha-release-0-1-3/

Posted in .NET, Code, Mobile | Tagged: , , , | Leave a Comment »

How to Kill a Windows NT Service in command prompt

Posted by runebune on April 24, 2009


Go to the command-prompt and query the service (e.g. the SMTP service) by using sc:

sc queryex SMTPSvc
This will give you the following information:

SERVICE_NAME: SMTPSvc
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 4 RUNNING
(STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
PID : 388
FLAGS :

or something like this (the “state” will mention stopping).
Over here you can find the process identifier (PID), so it’s pretty easy to kill the associated process either by using the task manager or by using taskkill:

taskkill /PID 388 /F

where the /F flag is needed to force the process kill (first try without the flag).

Posted in Code, Tips & Tricks | Tagged: , , | 1 Comment »

Installing uTorrent as windows service with webUI

Posted by runebune on April 24, 2009


this is the solution, and almost any program can be run as a windows service this way.

http://www.geekzilla.co.uk/View838302ED-E806-4314-AC3A-89872D6F8C9B.htm

Posted in Code, Tips & Tricks | Tagged: , , | 1 Comment »

HowTo get Microsoft Windows user or group SID in MSSQL

Posted by runebune on April 24, 2009


Microsoft Windows user or group can have different names in different languages. this is why a SID is used.

SQL Server 2005 Books Online (September 2007)
SUSER_SNAME (Transact-SQL)

Returns the login name associated with a security identification number (SID).

Syntax

SUSER_SNAME ( [ server_user_sid ] )
Arguments

server_user_sid
Is
the login security identification number. server_user_sid, which is
optional, is varbinary(85). server_user_sid can be the security
identification number of any SQL Server login or Microsoft Windows user
or group. If server_user_sid is not specified, information about the
current user is returned.

Return Types

nvarchar(128)

Remarks

SUSER_SNAME
can be used as a DEFAULT constraint in either ALTER TABLE or CREATE
TABLE. SUSER_SNAME can be used in a select list, in a WHERE clause, and
anywhere an expression is allowed. SUSER_SNAME must always be followed
by parentheses, even if no parameter is specified.

When called
without an argument, SUSER_SNAME returns the name of the current
security context. When called without an argument within a batch that
has switched context by using EXECUTE AS, SUSER_SNAME returns the name
of the impersonated context. When called from an impersonated context,
ORIGINAL_LOGIN returns the name of the original context.

Examples

A. Using SUSER_SNAME
The following example returns the login name for the security identification number with a value of 0x01.

Copy Code
SELECT SUSER_SNAME(0x01);
GO
B. Using SUSER_SNAME with a Windows user security ID
The following example returns the login name associated with a Windows security identification number.

Copy Code
SELECT SUSER_SNAME(0x010500000000000515000000a065cf7e784b9b5fe77c87705a2e0000);
GO
C. Using SUSER_SNAME as a DEFAULT constraint
The following example uses SUSER_SNAME as a DEFAULT constraint in a CREATE TABLE statement.

Copy Code
USE AdventureWorks;
GO
CREATE TABLE sname_example
(
login_sname sysname DEFAULT SUSER_SNAME(),
employee_id uniqueidentifier DEFAULT NEWID(),
login_date datetime DEFAULT GETDATE()
)
GO
INSERT sname_example DEFAULT VALUES
GO
D. Calling SUSER_SNAME in combination with EXECUTE AS
This example shows the behavior of SUSER_SNAME when called from an impersonated context.

SELECT SUSER_SNAME();

GO

EXECUTE AS LOGIN = ‘WanidaBenShoof’;

SELECT SUSER_SNAME();

REVERT;

GO

SELECT SUSER_SNAME();

GO

Here is the result.

sa

WanidaBenShoof

sa

———————————
To get the SID I use

sp_helplogins -shows information for all sql server users, incl. sid
sp_help -shows all buildin funktions (sp, tables …)

Posted in Code, Tips & Tricks | Tagged: , | Leave a Comment »

C# tip: move forms without clicking on the top bar

Posted by runebune on April 24, 2009


This is a way to move a form without clicking on the top bar.
This code snippet shows how a form could look like:


private bool _mouseDownClick;

public bool MouseDownClick
{
get { return _mouseDownClick; }
set { _mouseDownClick = value; }
}

private Point _mouseOffset;

public Point MouseOffset
{
get { return _mouseOffset; }
set { _mouseOffset = value; }
}

public DropBoxForm()
{
InitializeComponent();
}

private void DropBoxForm_MouseMove(object sender, MouseEventArgs e)
{
if (this.MouseDownClick)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(this.MouseOffset.X, this.MouseOffset.Y);
this.Location = mousePos;
}
}

private void DropBoxForm_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.MouseDownClick = false;
}
}

private void DropBoxForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.MouseOffset = new Point(-e.X, -e.Y);
this.MouseDownClick = true;
}
}

Posted in .NET, Code, Tips & Tricks | Tagged: | Leave a Comment »