DllImport 属性の使用 printui.dll を使用して、通常使うプリンターを名前で設定します rundll32 printui.dll,PrintUIEntry /? で、コマンドラインからの使用方法のダイアログが表示されます。 通常使うプリンターとして設定: rundll32 printui.dll,PrintUIEntry /y /n "printer" VB.net
Imports System.Runtime.InteropServices
Module Module1
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
Public Function LoadLibraryA( _
<MarshalAs(UnmanagedType.LPStr)> ByVal DllName As String _
) As Integer
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
Public Function FreeLibrary( _
ByVal hModule As Integer _
) As Integer
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
Public Function GetProcAddress( _
ByVal hModule As Integer, _
<MarshalAs(UnmanagedType.LPStr)> ByVal lpProcName As String _
) As Integer
End Function
<DllImport("user32.dll", CharSet:=CharSet.Unicode)> _
Public Function GetDesktopWindow() As Integer
End Function
Delegate Sub PrintUIEntryW( _
ByVal hwnd As Int32, _
ByVal hinst As Int32, _
<MarshalAs(UnmanagedType.LPWStr)> ByVal text As String, _
ByVal nCmdShow As Int32)
Sub Main()
Dim hModule As Integer = LoadLibraryA("printui.dll")
' *****************************
' 実行
' *****************************
Dim ptr As IntPtr
ptr = GetProcAddress(hModule, "PrintUIEntryW")
If ptr <> IntPtr.Zero Then
Dim func1 As PrintUIEntryW = _
Marshal.GetDelegateForFunctionPointer(ptr, _
GetType(PrintUIEntryW) _
)
Call func1(GetDesktopWindow(), hModule, _
"/y /n """ & "プリンタ名" & """", 5)
Call FreeLibrary(hModule)
End If
End Sub
End Module
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace printui_cs {
class Program {
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int LoadLibraryA([MarshalAs(UnmanagedType.LPStr)]
string DllName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int FreeLibrary(int hModule);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetProcAddress(int hModule,
[MarshalAs(UnmanagedType.LPStr)]
string lpProcName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetDesktopWindow();
public delegate void PrintUIEntryW(
Int32 hwnd,
Int32 hinst,
[MarshalAs(UnmanagedType.LPWStr)] string text,
Int32 nCmdShow);
static void Main(string[] args) {
int hModule = LoadLibraryA("printui.dll");
// **************************
// 実行
// **************************
IntPtr ptr;
ptr = (IntPtr)GetProcAddress(hModule, "PrintUIEntryW");
if (ptr != IntPtr.Zero) {
PrintUIEntryW func1 =
(PrintUIEntryW)Marshal.GetDelegateForFunctionPointer(ptr, typeof(PrintUIEntryW));
func1(GetDesktopWindow(), hModule, "/y /n \"" + "プリンタ名" + "\"", 5);
}
FreeLibrary(hModule);
}
}
}
※ "プリンタ名" が デバイスとプリンターで表示されるプリンタ名です。 関連する Microsoft ドキュメント ユーザーによる操作なしで Windows にプリンタを追加する方法 [INFO] Windows の Rundll と Rundll32 インターフェイス UnmanagedType 列挙体 文字列に対する既定のマーシャリング PrintUIEntryW の最後の引数は、ShowWindow の nCndShow と同じで、5 は SW_SHOW を意味します。
|
|
【C#の最新記事】
- C# : PHP と連携してバイナリデータを WebClient.UploadDataAsync でそのままアップロードする
- C# : WebClient で JSON データを取得後 Json.NET でオブジェクト化( ついでに PropertyInfo でプロパティデータ一覧を foreach で取得 )
- C# : Microsoft Access の接続で、他の RDBMS( ここでは MySQL ) にエクスポートを行う
- C# : TKMP.DLLを使った、Gmail 用メール送信テンプレート
- C# : DataGridView を使用したナチュラルな行データの更新
- C# の delegate : メソッドの引数にメソッドを渡して使用する方法と、JavaScript の function(){} と同じ使用方法( 匿名 )
- PowerShell 移行用 C# コンソールアプリのコードテスト( ZIP 圧縮 )
- 解く事が目的では無い、身に付ける事が目的の C# 初心者用の問題を作ったので良かったらどうぞ (4)
- 解く事が目的では無い、身に付ける事が目的の C# 初心者用の問題を作ったので良かったらどうぞ (3)
- 解く事が目的では無い、身に付ける事が目的の C# 初心者用の問題を作ったので良かったらどうぞ (2)
- 解く事が目的では無い、身に付ける事が目的の C# 初心者用の問題を作ったので良かったらどうぞ (1)
- ComboBox : C# : Form アプリケーションで良く使うコントロール / VS2012 にて
- C# バッチビルドキットで、VB の My 名前空間を使ってクリップボードを使ったり、キーボードの SHIFT キーが押されているかを知る
- LINQ で List
のソート - VS2010(C#)WPF : ListView 内に GridView を配置する
- Windows ストア用の System.Collections.Generic 名前空間には、SortedList はありません。
- 情報が全くみつからない Microsoft.Live
- VS2010(C#) バッチ(コンソールアプリ) Twitter 投稿
- Windows8(C#) の WebView の LoadComplete で取得した URL 内のアクセストークンを Split で取得
- Facebook C# SDK の 5.4.1.0 のダウンロード




























