質問するログイン新規登録

質問編集履歴

2

動くコードに変更

2018/11/07 05:00

投稿

aqaxliy
aqaxliy

スコア13

title CHANGED
File without changes
body CHANGED
@@ -7,70 +7,72 @@
7
7
  しかし、ハンドルなどを利用した事が今までないので、
8
8
  どうすれば良いか殆どわからない状態で作ったコードが下記のコードになります。
9
9
 
10
+ ### 該当のソースコード
10
11
 
12
+ ```vb
13
+ <StructLayout(LayoutKind.Sequential)> _
11
- ### 発生している問題・エラーメッセージ
14
+ Public Structure DEVMODE
15
+ Public Const CCHDEVICENAME As Integer = 32
16
+ Public Const CCHFORMNAME As Integer = 32
12
17
 
13
- ```
14
- //下記コードで発生
15
- //Dim gch As GCHandle = GCHandle.Alloc(devmodeStructure, GCHandleType.Pinned)
18
+ <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCHDEVICENAME)> _
19
+ Public dmDeviceName As String
20
+ Public dmSpecVersion As Short
21
+ Public dmDriverVersion As Short
22
+ Public dmSize As Short
23
+ Public dmDriverExtra As Short
24
+ Public dmFields As Integer
16
25
 
17
- 'System.ArgumentException' のハンドルされていない例外が mscorlib.dll で発生しました
26
+ Public dmOrientation As Short
27
+ Public dmPaperSize As Short
28
+ Public dmPaperLength As Short
29
+ Public dmPaperWidth As Short
18
30
 
31
+ Public dmScale As Short
32
+ Public dmCopies As Short
33
+ Public dmDefaultSource As Short
34
+ Public dmPrintQuality As Short
35
+ Public dmColor As Short
36
+ Public dmDuplex As Short
37
+ Public dmYResolution As Short
38
+ Public dmTTOption As Short
39
+ Public dmCollate As Short
19
- 追加情報:オブジェクトに、プリミティブでないか、または blittable でないデータが含まれています。
40
+ <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCHFORMNAME)> _
20
- ```
41
+ Public dmFormName As String
42
+ Public dmLogPixels As Short
43
+ Public dmBitsPerPel As Integer
44
+ Public dmPelsWidth As Integer
45
+ Public dmPelsHeight As Integer
46
+ Public dmDisplayFlags As Integer
47
+ Public dmDisplayFrequency As Integer
21
48
 
22
- ### 該当のソースコード
49
+ Public dmICMMethod As Integer
50
+ Public dmICMIntent As Integer
51
+ Public dmMediaType As Integer
52
+ Public dmDitherType As Integer
53
+ Public dmReserved1 As Integer
54
+ Public dmReserved2 As Integer
55
+ Public dmPanningWidth As Integer
56
+ Public dmPanningHeight As Integer
23
57
 
24
- ```vb
25
- Public Structure DEVMODE
26
- Public strDeviceName As String
27
- Public intSpecVersion As Integer
28
- Public intDriverVersion As Integer
29
- Public intSize As Integer
30
- Public intDriverExtra As Integer
31
- Public lngFields As Long
32
- Public intOrientation As Integer
33
- Public intPaperSize As Integer
34
- Public intPaperLength As Integer
35
- Public intPaperWidth As Integer
36
- Public intScale As Integer
37
- Public intCopies As Integer
38
- Public intDefaultSource As Integer
39
- Public intPrintQuality As Integer
40
- Public intColor As Integer
41
- Public intDuplex As Integer
42
- Public intResolution As Integer
43
- Public intTTOption As Integer
58
+ Public dmPositionX As Integer
44
- Public intCollate As Integer
59
+ Public dmPositionY As Integer
45
- Public strFormName As String
46
- Public lngPad As Long
47
- Public lngBits As Long
48
- Public lngPW As Long
49
- Public lngPH As Long
50
- Public lngDFI As Long
51
- Public lngDFr As Long
52
60
  End Structure
53
61
 
54
62
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
55
- Dim devmodeStructure As New DEVMODE
56
-
57
- 'ガベージコレクタがオブジェクトを移動できないようにする
58
- Dim gch As GCHandle = GCHandle.Alloc(devmodeStructure, GCHandleType.Pinned)
59
- '固定オブジェクトのアドレスを取得する
60
- Dim address As Integer = gch.AddrOfPinnedObject().ToInt32()
61
-
62
63
  PrintDocument1.PrinterSettings.PrinterName = "Microsoft Print to PDF"
63
- PrintDocument1.PrinterSettings.DefaultPageSettings.CopyToHdevmode(address)
64
+ Dim devmodePtr As IntPtr = PrintDocument1.PrinterSettings.GetHdevmode()
64
-
65
- 'ハンドルを解放する
65
+ Dim dm As DEVMODE = CType(Marshal.PtrToStructure(devmodePtr, GetType(DEVMODE)), DEVMODE)
66
- gch.Free()
66
+ GlobalFree(devmodePtr)
67
67
  End Sub
68
68
  ```
69
69
 
70
- ### たこと
70
+ ### 発生ている問題・エラーメッセージ
71
71
 
72
- DEVMODE構造体からString型変数消せば取り合えず例外は発生しませんでした。
72
+ 上記コード確認すると、
73
- DEVMODE構造体印刷情報も入ってきませんでしが・・・
73
+ dm.dmDeviceNameが「K」ってり、
74
+ dm.dmPaperSizeが「101」になっていたり、
75
+ 上手く取得できません。
74
76
 
75
77
  ### 補足情報(FW/ツールのバージョンなど)
76
78
 

1

誤字

2018/11/07 05:00

投稿

aqaxliy
aqaxliy

スコア13

title CHANGED
File without changes
body CHANGED
@@ -21,7 +21,7 @@
21
21
 
22
22
  ### 該当のソースコード
23
23
 
24
- ```vb.net
24
+ ```vb
25
25
  Public Structure DEVMODE
26
26
  Public strDeviceName As String
27
27
  Public intSpecVersion As Integer