質問編集履歴

2

動くコードに変更

2018/11/07 05:00

投稿

aqaxliy
aqaxliy

スコア13

test CHANGED
File without changes
test CHANGED
@@ -16,89 +16,105 @@
16
16
 
17
17
 
18
18
 
19
-
20
-
21
- ### 発生している問題・エラーメッセージ
22
-
23
-
24
-
25
- ```
26
-
27
- //下記コードで発生
28
-
29
- //Dim gch As GCHandle = GCHandle.Alloc(devmodeStructure, GCHandleType.Pinned)
30
-
31
-
32
-
33
- 型 'System.ArgumentException' のハンドルされていない例外が mscorlib.dll で発生しました
34
-
35
-
36
-
37
- 追加情報:オブジェクトに、プリミティブでないか、または blittable でないデータが含まれています。
38
-
39
- ```
40
-
41
-
42
-
43
19
  ### 該当のソースコード
44
20
 
45
21
 
46
22
 
47
23
  ```vb
48
24
 
25
+ <StructLayout(LayoutKind.Sequential)> _
26
+
49
27
  Public Structure DEVMODE
50
28
 
51
- Public strDeviceName As String
29
+ Public Const CCHDEVICENAME As Integer = 32
52
30
 
53
- Public intSpecVersion As Integer
31
+ Public Const CCHFORMNAME As Integer = 32
54
32
 
55
- Public intDriverVersion As Integer
56
33
 
57
- Public intSize As Integer
58
34
 
59
- Public intDriverExtra As Integer
35
+ <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCHDEVICENAME)> _
60
36
 
61
- Public lngFields As Long
37
+ Public dmDeviceName As String
62
38
 
63
- Public intOrientation As Integer
39
+ Public dmSpecVersion As Short
64
40
 
65
- Public intPaperSize As Integer
41
+ Public dmDriverVersion As Short
66
42
 
67
- Public intPaperLength As Integer
43
+ Public dmSize As Short
68
44
 
69
- Public intPaperWidth As Integer
45
+ Public dmDriverExtra As Short
70
46
 
71
- Public intScale As Integer
47
+ Public dmFields As Integer
72
48
 
73
- Public intCopies As Integer
74
49
 
75
- Public intDefaultSource As Integer
76
50
 
77
- Public intPrintQuality As Integer
51
+ Public dmOrientation As Short
78
52
 
79
- Public intColor As Integer
53
+ Public dmPaperSize As Short
80
54
 
81
- Public intDuplex As Integer
55
+ Public dmPaperLength As Short
82
56
 
83
- Public intResolution As Integer
57
+ Public dmPaperWidth As Short
84
58
 
85
- Public intTTOption As Integer
86
59
 
87
- Public intCollate As Integer
88
60
 
89
- Public strFormName As String
61
+ Public dmScale As Short
90
62
 
91
- Public lngPad As Long
63
+ Public dmCopies As Short
92
64
 
93
- Public lngBits As Long
65
+ Public dmDefaultSource As Short
94
66
 
95
- Public lngPW As Long
67
+ Public dmPrintQuality As Short
96
68
 
97
- Public lngPH As Long
69
+ Public dmColor As Short
98
70
 
99
- Public lngDFI As Long
71
+ Public dmDuplex As Short
100
72
 
73
+ Public dmYResolution As Short
74
+
75
+ Public dmTTOption As Short
76
+
101
- Public lngDFr As Long
77
+ Public dmCollate As Short
78
+
79
+ <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCHFORMNAME)> _
80
+
81
+ Public dmFormName As String
82
+
83
+ Public dmLogPixels As Short
84
+
85
+ Public dmBitsPerPel As Integer
86
+
87
+ Public dmPelsWidth As Integer
88
+
89
+ Public dmPelsHeight As Integer
90
+
91
+ Public dmDisplayFlags As Integer
92
+
93
+ Public dmDisplayFrequency As Integer
94
+
95
+
96
+
97
+ Public dmICMMethod As Integer
98
+
99
+ Public dmICMIntent As Integer
100
+
101
+ Public dmMediaType As Integer
102
+
103
+ Public dmDitherType As Integer
104
+
105
+ Public dmReserved1 As Integer
106
+
107
+ Public dmReserved2 As Integer
108
+
109
+ Public dmPanningWidth As Integer
110
+
111
+ Public dmPanningHeight As Integer
112
+
113
+
114
+
115
+ Public dmPositionX As Integer
116
+
117
+ Public dmPositionY As Integer
102
118
 
103
119
  End Structure
104
120
 
@@ -106,29 +122,13 @@
106
122
 
107
123
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
108
124
 
109
- Dim devmodeStructure As New DEVMODE
110
-
111
-
112
-
113
- 'ガベージコレクタがオブジェクトを移動できないようにする
114
-
115
- Dim gch As GCHandle = GCHandle.Alloc(devmodeStructure, GCHandleType.Pinned)
116
-
117
- '固定オブジェクトのアドレスを取得する
118
-
119
- Dim address As Integer = gch.AddrOfPinnedObject().ToInt32()
120
-
121
-
122
-
123
125
  PrintDocument1.PrinterSettings.PrinterName = "Microsoft Print to PDF"
124
126
 
125
- PrintDocument1.PrinterSettings.DefaultPageSettings.CopyToHdevmode(address)
127
+ Dim devmodePtr As IntPtr = PrintDocument1.PrinterSettings.GetHdevmode()
126
128
 
129
+ Dim dm As DEVMODE = CType(Marshal.PtrToStructure(devmodePtr, GetType(DEVMODE)), DEVMODE)
127
130
 
128
-
129
- 'ハンドルを解放する
130
-
131
- gch.Free()
131
+ GlobalFree(devmodePtr)
132
132
 
133
133
  End Sub
134
134
 
@@ -136,13 +136,17 @@
136
136
 
137
137
 
138
138
 
139
- ### たこと
139
+ ### 発生ている問題・エラーメッセージ
140
140
 
141
141
 
142
142
 
143
- DEVMODE構造体からString型変数消せば取り合えず例外は発生しませんでした。
143
+ 上記コード確認すると、
144
144
 
145
- DEVMODE構造体印刷情報も入ってきませんでしが・・・
145
+ dm.dmDeviceNameが「K」ってり、
146
+
147
+ dm.dmPaperSizeが「101」になっていたり、
148
+
149
+ 上手く取得できません。
146
150
 
147
151
 
148
152
 

1

誤字

2018/11/07 05:00

投稿

aqaxliy
aqaxliy

スコア13

test CHANGED
File without changes
test CHANGED
@@ -44,7 +44,7 @@
44
44
 
45
45
 
46
46
 
47
- ```vb.net
47
+ ```vb
48
48
 
49
49
  Public Structure DEVMODE
50
50