Created
March 22, 2015 07:25
-
-
Save hymkor/5cae2d722244596e3214 to your computer and use it in GitHub Desktop.
Go で Windows の ini を読む ref: http://qiita.com/zetamatta/items/e86deb7f6587500391c5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kernel32ini | |
import ( | |
"syscall" | |
"unsafe" | |
) | |
var kernel32 = syscall.NewLazyDLL("kernel32") | |
var getPrivateProfileString = kernel32.NewProc("GetPrivateProfileStringW") | |
func GetPrivateProfileString(path, section, keyname, defaultValue string) (string, error) { | |
var buffer [2048]uint16 | |
sec16, secErr := syscall.UTF16PtrFromString(section) | |
if secErr != nil { | |
return "", secErr | |
} | |
key16, keyErr := syscall.UTF16PtrFromString(keyname) | |
if keyErr != nil { | |
return "", keyErr | |
} | |
default16, defaultErr := syscall.UTF16PtrFromString(defaultValue) | |
if defaultErr != nil { | |
return "", defaultErr | |
} | |
path16, pathErr := syscall.UTF16PtrFromString(path) | |
if pathErr != nil { | |
return "", pathErr | |
} | |
result, _, err := getPrivateProfileString.Call( | |
uintptr(unsafe.Pointer(sec16)), | |
uintptr(unsafe.Pointer(key16)), | |
uintptr(unsafe.Pointer(default16)), | |
uintptr(unsafe.Pointer(&buffer[0])), | |
uintptr(len(buffer)), | |
uintptr(unsafe.Pointer(path16))) | |
if result <= 0 { | |
return "", err | |
} | |
return syscall.UTF16ToString(buffer[0:result]), nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kernel32ini | |
import ( | |
"fmt" | |
"os" | |
"path" | |
"testing" | |
) | |
const ( | |
INIPATH = "getprivateprofilestring.ini" | |
SECTION = "section1" | |
KEY = "key1" | |
DEFAULT = "" | |
) | |
func TestGetPrivateProfileString(t *testing.T) { | |
wd, err := os.Getwd() | |
if err != nil { | |
fmt.Println(err.Error()) | |
t.Fail() | |
return | |
} | |
iniPath := path.Join(wd, INIPATH) | |
value, err := GetPrivateProfileString(iniPath, SECTION, KEY, DEFAULT) | |
if err != nil { | |
fmt.Println(err.Error()) | |
t.Fail() | |
return | |
} | |
fmt.Printf("%s[%s]%s=%s\n", iniPath, SECTION, KEY, value) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment