• ベストアンサー

WNetAddConnection2で困っています

お世話になります。 VC6.0のサービス内でのネットワーク接続関数:WNetAddConnection2で困っています。 パラメータ NETRESOURCE nr;の // ローカルドライブ名 nr.lpLocalName = ""; // 共有フォルダ名 nr.lpRemoteName = "\\\\saver\\c$"; がエラーになります 「2 番目の引数を 'char [6]' から 'const unsigned short *' に変換できません。 (新しい機能 ; ヘルプを参照)指示された型は関連がありません; 変換にはreinterpret_cast、 C スタイル キャストまたは関数スタイルのキャストが必要です。」 (unsign short *)のセット方法は下記のように直したのですが // 資源の種類 nr.dwType = RESOURCETYPE_DISK; // ローカルドライブ名 nr.lpLocalName = (unsigned short*)""; // 共有フォルダ名 nr.lpRemoteName = (unsigned short*)"\\\\saver\\c$"; // プロバイダー名 nr.lpProvider = NULL; ・・・ dwret = WNetAddConnection2( &nr,(const unsigned short *)"passdw",(const unsigned short *)"userid",0); コンパイルはとおったのですが 接続できません (unsign short *)のセット方法が悪いのか サービスでなければうまく動くのですが・・・。 サービスなのでデバックもよくできません アドバイス、回答、お願いします。

  • phoo
  • お礼率85% (107/125)

質問者が選んだベストアンサー

  • ベストアンサー
  • nakashi
  • ベストアンサー率51% (21/41)
回答No.3

nr.lpLocalName = L""; nr.lpRemoteName = L"\\\\saver\\c$"; nr.lpProvider = NULL; dwret = WNetAddConnection2( &nr,L"passdw",L"userid",0); では? ASCII(char*)型にするなら 方法1 #define UNICODE #define _UNICODE を検索してコメントにする。 方法2 NETRESOURCEA構造体と WNetAddConnection2A関数を使う

phoo
質問者

お礼

回答、ありがとうございます。

その他の回答 (2)

  • a-kuma
  • ベストアンサー率50% (1122/2211)
回答No.2

Windows 系はあまりプログラムを組まないので良く分かりませんが、Unicode を 使っているから、じゃないですか? > nr.lpRemoteName = "\\\\saver\\c$"; nr.lpRemoteName = _T("\\\\saver\\c$"); って感じではいかが? ちなみに、「型が違うよ」と言っているエラーに対して、理由も分からずキャスト をしても問題の解決になることは、ほとんどありませんよ。

phoo
質問者

お礼

回答ありがとうございます。

  • takebe
  • ベストアンサー率65% (17/26)
回答No.1

# あてずっぽうですが... サービスというのはNTとかWindows2000のサービスのことでしょうか. サービスを動かすアカウントの権限と普通のコマンドを動かすアカウントの権限が違っているため,接続ができないのかもしれません. サービスを動かすアカウントをコマンドを動かしているアカウントに変更してみたらうまく動いたりしませんかね.

phoo
質問者

お礼

早々の回答ありがとうございました。

関連するQ&A

  • C言語のキャストについて

    お世話になります。 CRC-16の計算プログラムをC言語でつくりました。 例えば・・・1F 08 00 00 12 34 なら“1F0800001234”と入力すると【EEC2】と表示するプログラムです。 ただ・・・.Net SDKでコンパイルするとできたのですが、Visual C++2008でコンパイルするとエラーが出てしまいます。 (48) : error C2664: 'strlen' : 1 番目の引数を 'unsigned char [256]' から 'const char *' に変換できません。(新しい機能 ; ヘルプを参照) 1> 指示された型は関連がありません。変換には reinterpret_cast、C スタイル キャストまたは関数スタイルのキャストが必要です。 (52) : error C2664: 'strtol' : 1 番目の引数を 'unsigned char [3]' から 'const char *' に変換できません。(新しい機能 ; ヘルプを参照) 1> 指示された型は関連がありません。変換には reinterpret_cast、C スタイル キャストまたは関数スタイルのキャストが必要です。 型変換が必要ってことまではわかったのですが・・・必要なのはわかって行き詰まり状態です。。。 どのようしたらよいのでしょうか?ご教授をよろしくお願いします。 ソースは以下の通りです。 #include "stdafx.h" #include <stdio.h> #include <string.h> #include <stdlib.h> unsigned short crc_cal(unsigned short lng, unsigned char *str) { unsigned short crc, i, j, t; t= 0x0000; crc = 0xffff; for (i = 0; i < lng ; i++) { crc ^= (unsigned short) str[i]; t = (unsigned short) str[i]; for (j = 1; j <= 8; j++) { if (crc & 1) { // carry bit on crc = crc >> 1; crc ^= 0xa001; } else { // carry bit off crc = crc >> 1; } } } return crc; } int main(void) { unsigned char str[256],data[128],hexstr[3]; unsigned short crc,CRC,len; while(1) { printf("Please input key (HEX)\n"); scanf("%255s",str); hexstr[2]='\0'; for(len=0; len<(strlen(str)/2) ;len++) { hexstr[0]=str[len*2]; hexstr[1]=str[len*2+1]; data[len]=(unsigned char)strtol(hexstr, NULL, 16); } crc = crc_cal(len,data); CRC = (crc>>8) | (crc<<8); printf("\nCRC16 = %04X\n\n", CRC); } return 0; }

  • valatile と多重代入の関連

    a=b=c=10; というような代入の方法がありますね。 これを多重代入と呼ぶかどうか知らないのですが、仮にそのように呼ぶことにします。 この多重代入の代入先がハードウエアのポートのようなとき、通常volatile修飾を付けます。 static volatile unsigned short *const REGS [] = { (volatile unsigned short*) BASE, (volatile unsigned short*) (BASE+0x20), (volatile unsigned short*) (BASE+0x40)}; のようだったとします。 上記のアドレスに *(REGS [0]) = *(REGS [1]) = *(REGS [2]) = 0; のように初期化を行ったとき、ある処理系では全てのアドレスに0を書き込み、 別な処理系では*(REGS [2])の内容をいったんリードし、それを*(REGS [1])に書き込みました。 どちらがC言語の仕様に忠実なのでしょうか? よろしくお願いいたします

  • 最近のCPUのほとんどはリトルエンディアンかビッグエンディアンでしょうか?

    2000年あたり以降に出た Windows, Mac, Linuxに使われているCPUのほとんどはリトルエンディアンかビッグエンディアンでしょうか? また、たとえば以下のような方法でエンディアンを調べられると考えていいのでしょうか?(VC++です) #include <windows.h> void GetEndian4(char* c){ unsigned __int32 a=0x03020100; BYTE *b = (BYTE*)&a, i=4; while (i--) c[i]=b[i]; } ////////// const char e[4]={}; GetEndian4( const_cast<char*>(e) ); //eが 0,1,2,3 になればリトルエンディアン //3,2,1,0 になればビッグエンディアン //PDP-エンディアンだと 2,3,0,1 …のはず (または2択ならこれだけでも判断可能…?) short s=1; printf( *(char*)&s ? "リトルエディアン\n" : "ビッグエディアン\n" ); あとここでもアラインメントの問題が絡みますが、このように アラインメントが(2のべき乗だとして)大きいであろう方から小さいであろう方にキャストする分には安全で、逆に sizeof(short) == sizeof(char)*2 として char c[2]={1,0}; short s=*(short*)&c; というのは危険な場合がある、ということでしょうか? また、その場合は たとえばビッグエンディアンなら short s=(c[0]<<8)|c[1]; とすればいいでしょうか?

  • C言語:アウトプット引数のキャストについて

    以下のサンプルソースのように、(1)の場合、出力結果が24にならないのはなぜでしょうか?基礎的な質問だと思いますがよろしくお願いします。 --------------------------------------- サンプルソース #include <stdio.h> int getValue(unsigned short* us){ *us = 24; } main(){ int ret = 0; int intVal = 0; unsigned short usVal = 0; /* (1)int型のintValをunsigned short型にキャストした場合 */ ret = getValue((unsigned short*)&intVal); printf("%d\n", intVal); /* (2)unsigned short型のusValをそのまま引数に渡した場合 */ ret = getValue(&usVal); printf("%d\n", usVal); } ---------------------------------- 出力結果 1572864 ←(1)の結果 24 ←(2)の結果 -----------------------------------

  • VC++でコンパイルエラーが出ますがお教えください。

    初心者で申し訳なくおもいます。 下記のソースでコンパイルエラーが出ます。 // CnstDst.cpp : アプリケーション用のエントリ ポイントの定義 // #include "stdafx.h" //CnstDst クラス定義 class CnstDst { char sDat[80]; public: CnstDst(char *s); ~CnstDst(); }; CnstDst cd1("テストです"); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // TODO: この位置にコードを記述してください。 MessageBox(NULL, "WinMainの入口", "WinMain", MB_OK); CnstDst cd2("テスト1"); MessageBox(NULL, "テスト2", "WinMain", MB_OK); return 0; } CnstDst::CnstDst(char *s) { lstrcpy(sDat, s); MessageBox(NULL, sDat, "コンストラクタ", MB_OK); } CnstDst::~CnstDst() { MessageBox(NULL, sDat, "デストラクタ", MB_OK); } コンパパイルエラーは下記です。 プロジェクト 'CnstDst - Win32 (WCE MIPSII_FP) Debug' 用の中間ファイルおよび出力ファイルを削除しています。 --------------------構成 : CnstDst - Win32 (WCE MIPSII_FP) Debug-------------------- リソースをコンパイル中... コンパイル中... StdAfx.cpp コンパイル中... CnstDst.cpp C:\Documents and Settings\中野\デスクトップ\テストフォルダ\CnstDst\CnstDst.cpp(22) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [14]' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast C:\Documents and Settings\中野\デスクトップ\テストフォルダ\CnstDst\CnstDst.cpp(24) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [9]' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast C:\Documents and Settings\中野\デスクトップ\テストフォルダ\CnstDst\CnstDst.cpp(31) : error C2664: 'wcscpy' : cannot convert parameter 1 from 'char [80]' to 'unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast C:\Documents and Settings\中野\デスクトップ\テストフォルダ\CnstDst\CnstDst.cpp(32) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [80]' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast C:\Documents and Settings\中野\デスクトップ\テストフォルダ\CnstDst\CnstDst.cpp(36) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [80]' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast clmips.exe の実行エラー CnstDst.exe - エラー 5、警告 0 Microsoft eMbedded Visuai C++ Win32 WCE で行っております。

  • C#で型変換がうまくいきません。

    C#で型変換がうまくいきません。 short r; byte[] b = new byte[] {1,2,3}; r = (short)b[1] * (short)256; というコードを書くと、 型 'int' を 'short' に暗黙的に変換できません。明示的な変換が存在します。(cast が不足していないかどうかを確認してください) と出てしまいます。キャストしているのに何がいけないのか全くわかりません。 どうぞ宜しくお願いいたします。

  • SDKでのエラーなのですが。

    はじめまして。 SDKの勉強で猫でもわかる~のHPの第37章のサンプルを実行したのですが、 --------------------構成: 037 - Win32 Debug-------------------- コンパイル中... main.cpp D:\SDK\037\main.cpp(230) : error C2664: 'CallWindowProcA' : 1 番目の引数を 'int (__stdcall *)(void)' から 'long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)' に変換できません。 (新しい機能 ; ヘルプを参照) この変換には reinterpret_cast, C スタイル キャストまたは関数スタイルのキャストが必要です。 D:\SDK\037\main.cpp(248) : error C2664: 'CallWindowProcA' : 1 番目の引数を 'int (__stdcall *)(void)' から 'long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)' に変換できません。 (新しい機能 ; ヘルプを参照) この変換には reinterpret_cast, C スタイル キャストまたは関数スタイルのキャストが必要です。 cl.exe の実行エラー 037.exe - エラー 2、警告 0 というようなエラーが出てしまいました。 このエラーを回避するにはどのようにすればいいのでしょうか?

  • コンパイルエラーが理解できません

    初心者ですお教えください。 下記ソースをMicrosoft eMbedded Visual C++ でコンパイルしましたがコンパイルエラーが出て 理解できませんどなたかお教えください。 STANDARDSDK Win32 (WCE x86)Debug STANDARDSDK Emulator -------------------------------------------------- #include "windows.h" int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { SYSTEMTIME tm; char s[80]; int iRet; // 現在時刻を取得します。 GetLocalTime(&tm); // 表示する文字列を設定します。 if ((tm.wHour >= 0) && (tm.wHour < 12)) { lstrcpy(s, "おはです。"); } else if (tm.wHour < 17) { lstrcpy(s, "こんちは。"); } else { lstrcpy(s, "こんば。"); } // メッセージボックスに文字列を表示します。 iRet = MessageBox(NULL, s, "MsgBox", MB_OK); // 正常終了を意味する戻り値を返します。 return 0; } <コンパイルエラー内容です> プロジェクト 'MsgMain - Win32 (WCE x86) Debug' 用の中間ファイルおよび出力ファイルを削除しています。 --------------------構成 : MsgMain - Win32 (WCE x86) Debug-------------------- コンパイル中... MsgMain.cpp C:\Documents and Settings\中野\デスクトップ\MsgMain.cpp(7) : error C2731: 'WinMain' : function cannot be overloaded C:\Documents and Settings\中野\デスクトップ\MsgMain.cpp(3) : see declaration of 'WinMain' C:\Documents and Settings\中野\デスクトップ\MsgMain.cpp(17) : error C2664: 'wcscpy' : cannot convert parameter 1 from 'char [80]' to 'unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast C:\Documents and Settings\中野\デスクトップ\MsgMain.cpp(19) : error C2664: 'wcscpy' : cannot convert parameter 1 from 'char [80]' to 'unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast C:\Documents and Settings\中野\デスクトップ\MsgMain.cpp(21) : error C2664: 'wcscpy' : cannot convert parameter 1 from 'char [80]' to 'unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast C:\Documents and Settings\中野\デスクトップ\MsgMain.cpp(25) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [80]' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast cl.exe の実行エラー MsgMain.exe - エラー 5、警告 0 以上です宜しくお願いします。

  • C言語のキャスト

    キーボード操作で見たい画像を切り替えるというプログラムを作成しようとしているのですがエラーが起きてしまい出来ません。 例えば、キーボードで「a」と押せばAの画像が出るように文字列にAの画像のファイル名を代入して画像を表示させようと思ったのですが、 const char [2]' から 'char' に変換できません。 この変換には reinterpret_cast, C スタイル キャストまたは関数スタイルのキャストが必要です。 というエラーが出てきます。 キャストという言葉は聞いたことがあるのですが、よくわかりません。 だれか教えてください。お願いします。

  • 【髪型】女の子らしさと似合っている度ならどちら?

    こんにちは(*´ω`*) 最近ちまたでは「マニッシュショート」という髪型が流行っているようです。 ※↓こういうのだそうです。※  http://www.beauty-box.jp/style/short/506c.html ショート流行中なんだ~と思っていたところ、前田敦子サンが髪の毛をバッサリ!! そこで、ショートは男性的に見ていかがなものなのですか? ショートでも似合っていればかわいいですか? 女の子らしさと似合っている度ならどちらがいいのでしょうか(`・ω・´)? いろんな意見を聞かせてください(*´ω`*)

専門家に質問してみよう