- - PR -
DocumentPropertiesでプリンタの設定情報を取得したい
1
投稿者 | 投稿内容 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
投稿日時: 2008-10-28 17:15
お世話になります。
プリンタの設定情報を取得、設定しようとしてますが、うまくいきません。 (開発環境は、VB2005、XP) DocumentPropertiesで設定情報取得のポインタは取れるのですが、ポインタからDEVMODE5への構造体にコピー(PtrToStructure)ができません。 (ランタイムの重大なエラーが検出されました。というエラーメッセージ) いろんな方法で試したのですが、うまくいかず、PtrToStructureでも駄目という状態で困っています。 どなたか、力を貸してください。 よろしくお願いします。 以下関連するコード内容です。 Declare Function DocumentProperties Lib "winspool.drv" Alias "DocumentPropertiesA" (ByVal hwnd As IntPtr, ByVal hPrinter As Integer, <Runtime.InteropServices.MarshalAs(UnmanagedType.LPTStr)> ByVal pDeviceName As String, _ ByVal pDevModeOutput As IntPtr, _ ByVal pDevModeInput As IntPtr, _ ByVal fMode As Integer) As Integer Declare Function DocumentProperties Lib "winspool.drv" Alias "DocumentPropertiesA" (ByVal hwnd As IntPtr, ByVal hPrinter As Integer, <Runtime.InteropServices.MarshalAs(UnmanagedType.LPTStr)> ByVal pDeviceName As String, _ ByVal pDevModeOutput As Integer, _ ByVal pDevModeInput As Integer, _ ByVal fMode As Integer) As Integer /省略/ If OpenPrinter(PrinterName, PrinterHandle, Nothing) >= 0 Then nSize = DocumentProperties(IntPtr.Zero, PrinterHandle, PrinterName, 0, 0, 0) Dim hdmIn As IntPtr hdmIn = Marshal.AllocHGlobal(nSize) Dim intRet As Integer = DocumentProperties(IntPtr.Zero, PrinterHandle, PrinterName, hdmIn, IntPtr.Zero, DM_OUT_BUFFER) Dim inDev5 As New DEVMODE5 inDev5 = CType(Marshal.PtrToStructure(hdmIn, GetType(DEVMODE5)), DEVMODE5) /省略/ End If | ||||||||||||
|
投稿日時: 2008-11-13 22:57
エラーメッセージは重要な情報です。略さず、そのまま引用してください。 DEVMODE5 は、どのように定義されていますか?
こちらのコードですが、MSDN<microsoft.com>にある例を見ると、New 演算子は不要なようです。 | ||||||||||||
|
投稿日時: 2008-11-14 22:46
直接エラーとは関係ないと思いますが。 MSDN ライブラリに説明として上がっている関数の名前は、「DocumentProperties」だと思います。では、Alias として指定している「DocumentPropertiesA」とは、なんでしょう? そして、引数で指定している「MarshalAs(UnmanagedType.LPTStr)」は、どういう意味を持っているでしょうか。 多くの Win32 API は、文字列に対して ASCII コードで指定するものと、Unicode で指定するものの2つが実装されています。この2つを、「DocumentPropertiesA」「DocumentPropertiesW」のように、最後に "A" か "W" を付けて識別します。 しかし、コードを書くときに、そんなところまで意識したくはありません。そのため、Transfer するための型が定義されました。それが、TCHAR であったり、LPTSTR であったりです。これらの型は、_MBCS または _UNICODE が定義されているかどうかで、char, char* になったり、w_char, w_char* になったりします。 ここで、「DocumentPropertiesA」と、ANSII なコードをやりとりするタイプを指定しながら、「MarshalAs(UnmanagedType.LPTStr)」と、コンパイル時の環境依存なコードを指定しています。XP を使っていらっしゃるとのことなので、ここは Unicode になります。おかしくなることは、おわかりでしょうか。 |
1