This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_transfer_to_pc_test.go
82 lines (65 loc) · 1.77 KB
/
example_transfer_to_pc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package gowpd_test
import (
"github.com/rlj1202/go-wpd"
"log"
)
func Example_transferToPC() {
gowpd.Initialize()
mng, err := gowpd.CreatePortableDeviceManager()
if err != nil {
panic(err)
}
deviceIDs, err := mng.GetDevices()
if err != nil {
panic(err)
}
clientInfo, err := gowpd.CreatePortableDeviceValues()
if err != nil {
panic(err)
}
clientInfo.SetStringValue(gowpd.WPD_CLIENT_NAME, "libgowpd")
clientInfo.SetUnsignedIntegerValue(gowpd.WPD_CLIENT_MAJOR_VERSION, 1)
clientInfo.SetUnsignedIntegerValue(gowpd.WPD_CLIENT_MINOR_VERSION, 0)
clientInfo.SetUnsignedIntegerValue(gowpd.WPD_CLIENT_REVISION, 2)
// object ID which will be transferred to PC.
targetObjectID := "F:\\test.txt"
// location where file will be transferred into.
targetDestination := "E:\\test.txt"
for _, id := range deviceIDs {
portableDevice, err := gowpd.CreatePortableDevice()
if err != nil {
panic(err)
}
portableDevice.Open(id, clientInfo)
content, err := portableDevice.Content()
if err != nil {
panic(err)
}
resources, err := content.Transfer()
if err != nil {
panic(err)
}
objectDataStream, optimalTransferSize, err := resources.GetStream(targetObjectID, gowpd.WPD_RESOURCE_DEFAULT, gowpd.STGM_READ)
if err != nil {
panic(err)
}
pFinalFileStream, err := gowpd.SHCreateStreamOnFile(targetDestination, gowpd.STGM_CREATE|gowpd.STGM_WRITE)
if err != nil {
panic(err)
}
totalBytesWritten, err := gowpd.StreamCopy(pFinalFileStream, objectDataStream, optimalTransferSize)
if err != nil {
panic(err)
}
err = pFinalFileStream.Commit(0)
if err != nil {
panic(err)
}
log.Printf("Total bytes written: %d\n", totalBytesWritten)
gowpd.FreeDeviceID(id)
portableDevice.Release()
}
mng.Release()
gowpd.Uninitialize()
// Output:
}