std::wstring GameInfo::getSpecialPath(LPCWSTR name) const
{
  HKEY key;
  LONG errorcode = ::RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders",
                                   0, KEY_QUERY_VALUE, &key);

  if (errorcode != ERROR_SUCCESS) {
    throw windows_error("failed to look up special folder (path)", errorcode);
  }

  WCHAR temp[MAX_PATH];
  DWORD bufferSize = MAX_PATH;

  errorcode = ::RegQueryValueExW(key, name, nullptr, nullptr, (LPBYTE)temp, &bufferSize);
  if (errorcode != ERROR_SUCCESS) {
    throw windows_error((boost::format("failed to look up special folder (%1%)") % ToString(name, true)).str(), errorcode);
  }

  WCHAR temp2[MAX_PATH];
  // try to expand variables in the path, if any
  if (::ExpandEnvironmentStringsW(temp, temp2, MAX_PATH) != 0) {
    return temp2;
  } else {
    return temp;
  }
}
void serial_close (struct serial_channel* _chan)
{
	struct serial_win32*	chan = (struct serial_win32*)_chan;

	serial_destroy_struct(_chan);

	if ( ! CloseHandle(chan->hPort))
	{
		windows_error("serial_close, CloseHandle(hPort)");
	}

	if ( ! CloseHandle(chan->eRxReady))
	{
		windows_error("serial_close, CloseHandle(eRxReady)");
	}

	if ( ! CloseHandle(chan->eTxDone))
	{
		windows_error("serial_close, CloseHandle(eTxDone)");
	}

	if (chan->rxBuf)
	{
		free(chan->rxBuf);
	}

	free(chan);
}
Example #3
0
File: md5.cpp Project: Bekenn/wcdx
md5_hash::md5_hash(const void* data, size_t size)
{
    HCRYPTPROV provider;
    if (!::CryptAcquireContext(&provider, nullptr, nullptr, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
        throw windows_error();
    at_scope_exit([&]{ ::CryptReleaseContext(provider, 0); });

    HCRYPTHASH hash;
    if (!::CryptCreateHash(provider, CALG_MD5, 0, 0, &hash))
        throw windows_error();
    at_scope_exit([&]{ ::CryptDestroyHash(hash); });

    if (!::CryptHashData(hash, static_cast<const BYTE*>(data), size, 0))
        throw windows_error();

    DWORD hash_size;
    DWORD sz = sizeof(DWORD);
    if (!::CryptGetHashParam(hash, HP_HASHSIZE, reinterpret_cast<BYTE*>(&hash_size), &sz, 0))
        throw windows_error();

    if (hash_size != sizeof(*this))
        throw std::logic_error("Unexpected MD5 hash size: " + std::to_string(hash_size));

    sz = sizeof(*this);
    if (!::CryptGetHashParam(hash, HP_HASHVAL, reinterpret_cast<BYTE*>(this), &sz, 0))
        throw windows_error();
}
Example #4
0
long long file::size() const {
#ifdef _WIN32
  // Use GetFileSize instead of GetFileSizeEx for the case when _WIN32_WINNT
  // is less than 0x0500 as is the case with some default MinGW builds.
  // Both functions support large file sizes.
  DWORD size_upper = 0;
  HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd_));
  DWORD size_lower = FMT_SYSTEM(GetFileSize(handle, &size_upper));
  if (size_lower == INVALID_FILE_SIZE) {
    DWORD error = GetLastError();
    if (error != NO_ERROR)
      FMT_THROW(windows_error(GetLastError(), "cannot get file size"));
  }
  unsigned long long long_size = size_upper;
  return (long_size << sizeof(DWORD) * CHAR_BIT) | size_lower;
#else
  typedef struct stat Stat;
  Stat file_stat = Stat();
  if (FMT_POSIX_CALL(fstat(fd_, &file_stat)) == -1)
    FMT_THROW(system_error(errno, "cannot get file attributes"));
  static_assert(sizeof(long long) >= sizeof(file_stat.st_size),
      "return type of file::size is not large enough");
  return file_stat.st_size;
#endif
}
Example #5
0
/*!
  Playing starting at given MSF through analog output

  @param p_cdio the CD object to be acted upon.
*/
driver_return_code_t
audio_play_msf_win32ioctl (void *p_user_data, msf_t *p_start_msf,
                           msf_t *p_end_msf)
{
  const _img_private_t *p_env = p_user_data;
  CDROM_PLAY_AUDIO_MSF play;
  DWORD dw_bytes_returned;

  play.StartingM = cdio_from_bcd8(p_start_msf->m);
  play.StartingS = cdio_from_bcd8(p_start_msf->s);
  play.StartingF = cdio_from_bcd8(p_start_msf->f);

  play.EndingM   = cdio_from_bcd8(p_end_msf->m);
  play.EndingS   = cdio_from_bcd8(p_end_msf->s);
  play.EndingF   = cdio_from_bcd8(p_end_msf->f);

  bool b_success =
    DeviceIoControl(p_env->h_device_handle, IOCTL_CDROM_PLAY_AUDIO_MSF,
                    &play, sizeof(play), NULL, 0, &dw_bytes_returned, NULL);

  if ( ! b_success ) {
    windows_error(CDIO_LOG_INFO, GetLastError());
    return DRIVER_OP_ERROR;
  }
  return DRIVER_OP_SUCCESS;

}
Example #6
0
static int
winprint_start (struct winprint_data *wd, Tcl_Interp *interp, PRINTDLG *pd,
		const struct print_text_options *pto, int *cancelled)
{
  DOCINFO di;

  *cancelled = 0;

  wd->aborted = 0;

  /* We have no way to pass information to the abort function, so we
     need to use a global variable.  */
  abort_wd = wd;
  if (! SetAbortProc (pd->hDC, abort_function))
    {
      windows_error (interp, "SetAbortFunc");
      return TCL_ERROR;
    }

  di.cbSize = sizeof (DOCINFO);
  di.lpszDocName = pto->name;
  di.lpszOutput = NULL;
  di.lpszDatatype = NULL;
  di.fwType = 0;

  if (StartDoc (pd->hDC, &di) <= 0)
    {
      if (GetLastError () == ERROR_CANCELLED)
	*cancelled = 1;
      else
	{
	  windows_error (interp, "StartDoc");
	  return TCL_ERROR;
	}
    }

  return TCL_OK;
}
Example #7
0
/**
  Close the tray of a CD-ROM

  @param p_user_data the CD object to be acted upon.

*/
driver_return_code_t
close_tray_win32ioctl (const char *psz_win32_drive)
{
#ifdef WIN32
  DWORD dw_bytes_returned;
  DWORD dw_access_flags;

  OSVERSIONINFO ov;
  HANDLE h_device_handle;
  BOOL   b_success;

  memset(&ov,0,sizeof(OSVERSIONINFO));
  ov.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
  GetVersionEx(&ov);

  if((ov.dwPlatformId==VER_PLATFORM_WIN32_NT) &&
     (ov.dwMajorVersion>4))
    dw_access_flags = GENERIC_READ|GENERIC_WRITE;  /* add gen write on W2k/XP */
  else dw_access_flags = GENERIC_READ;

  h_device_handle = CreateFile( psz_win32_drive,
                                dw_access_flags,
                                FILE_SHARE_READ | FILE_SHARE_WRITE,
                                NULL,
                                OPEN_EXISTING,
                                0,
                                NULL );

  if( h_device_handle == INVALID_HANDLE_VALUE ) {
    return DRIVER_OP_ERROR;
  }

  b_success =
    DeviceIoControl(h_device_handle, IOCTL_STORAGE_LOAD_MEDIA2,
                    NULL, (DWORD) 0, NULL, 0, &dw_bytes_returned, NULL);


  CloseHandle(h_device_handle);

  if ( b_success != 0 ) {
    windows_error(CDIO_LOG_INFO, GetLastError());
    return DRIVER_OP_ERROR;
  }
  return DRIVER_OP_SUCCESS;
#else
  return DRIVER_OP_UNSUPPORTED;
#endif
}
Example #8
0
/*!
  Read Audio Subchannel information

  @param p_cdio the CD object to be acted upon.

*/
driver_return_code_t
audio_read_subchannel_win32ioctl (void *p_user_data,
                                  cdio_subchannel_t *p_subchannel)
{
  const _img_private_t *p_env = p_user_data;
  DWORD dw_bytes_returned;
  CDROM_SUB_Q_DATA_FORMAT q_data_format;
  SUB_Q_CHANNEL_DATA q_subchannel_data;

  q_data_format.Format = CDIO_SUBCHANNEL_CURRENT_POSITION;
  q_data_format.Track=0; /* Not sure if this has to be set or if so what
                            it should be. */

  if( ! DeviceIoControl( p_env->h_device_handle,
                       IOCTL_CDROM_READ_Q_CHANNEL,
                       &q_data_format, sizeof(q_data_format),
                       &q_subchannel_data, sizeof(q_subchannel_data),
                       &dw_bytes_returned, NULL ) ) {
    windows_error(CDIO_LOG_INFO, GetLastError());
    return DRIVER_OP_ERROR;
  }
  p_subchannel->audio_status =
    q_subchannel_data.CurrentPosition.Header.AudioStatus;
  p_subchannel->track =
    q_subchannel_data.CurrentPosition.TrackNumber;
  p_subchannel->index =
    q_subchannel_data.CurrentPosition.IndexNumber;
  p_subchannel->index =
    q_subchannel_data.CurrentPosition.IndexNumber;
  p_subchannel->address = q_subchannel_data.CurrentPosition.ADR;
  p_subchannel->control = q_subchannel_data.CurrentPosition.Control;

  {
    const UCHAR *abs_addr =
      q_subchannel_data.CurrentPosition.AbsoluteAddress;
    const UCHAR *rel_addr =
      q_subchannel_data.CurrentPosition.TrackRelativeAddress;

    p_subchannel->abs_addr.m = cdio_to_bcd8(abs_addr[1]);
    p_subchannel->abs_addr.s = cdio_to_bcd8(abs_addr[2]);
    p_subchannel->abs_addr.f = cdio_to_bcd8(abs_addr[3]);
    p_subchannel->rel_addr.m = cdio_to_bcd8(rel_addr[1]);
    p_subchannel->rel_addr.s = cdio_to_bcd8(rel_addr[2]);
    p_subchannel->rel_addr.f = cdio_to_bcd8(rel_addr[3]);
  }

  return DRIVER_OP_SUCCESS;
}
Example #9
0
/**
  Stop playing an audio CD.

  @param p_user_data the CD object to be acted upon.

*/
driver_return_code_t
audio_stop_win32ioctl (void *p_user_data)
{
  const _img_private_t *p_env = p_user_data;
  DWORD dw_bytes_returned;

  bool b_success =
    DeviceIoControl(p_env->h_device_handle, IOCTL_CDROM_STOP_AUDIO,
                    NULL, (DWORD) 0, NULL, 0, &dw_bytes_returned, NULL);

  if ( ! b_success ) {
    windows_error(CDIO_LOG_INFO, GetLastError());
    return DRIVER_OP_ERROR;
  }
  return DRIVER_OP_SUCCESS;
}
Example #10
0
/**
  Set the volume of an audio CD.

  @param p_user_data pointer to the CD object to be acted upon.
  @param p_volume pointer to the volume levels

*/
driver_return_code_t
audio_set_volume_win32ioctl (void *p_user_data,
                             /*in*/ cdio_audio_volume_t *p_volume)
{
  const _img_private_t *p_env = p_user_data;
  DWORD dw_bytes_returned;

  bool b_success =
    DeviceIoControl(p_env->h_device_handle, IOCTL_CDROM_SET_VOLUME,
                    p_volume, (DWORD) sizeof(cdio_audio_volume_t),
                    NULL, 0, &dw_bytes_returned, NULL);

  if ( ! b_success ) {
    windows_error(CDIO_LOG_INFO, GetLastError());
    return DRIVER_OP_ERROR;
  }
  return DRIVER_OP_SUCCESS;
}
void BasicWindow::create() {
	// Check whether the window already exists
	if(_window_handle)
		return;

	// Create the window
	_window_handle = CreateWindow(_window_class_name, // Custom window class
								  _window_title, // Window name
								  WS_OVERLAPPEDWINDOW, // usual window style
								  CW_USEDEFAULT, CW_USEDEFAULT, // Initial window position
								  _size_x, _size_y, // Initial window size
								  NULL, NULL, // no parent window and menu
								  _instance, this);

	if (!_window_handle)
		throw windows_error(L"Creating the window failed.");

	std::wclog << "Status: Window " << _window_title << " created." << std::endl;
}
Example #12
0
static int
winprint_finish (struct winprint_data *wd, Tcl_Interp *interp,
		 PRINTDLG *pd, int error)
{
  int ret;

  ret = TCL_OK;

  if (error || wd->aborted)
    AbortDoc (pd->hDC);
  else
    {
      if (EndDoc (pd->hDC) <= 0)
	{
	  windows_error (interp, "EndDoc");
	  ret = TCL_ERROR;
	}
    }

  DeleteDC (pd->hDC);

  return ret;
}
Example #13
0
/* Implement ide_winprint print_text.  */
static int
winprint_print_command (ClientData cd, Tcl_Interp *interp, int argc,
			     char **argv)
{
  struct winprint_data *wd = (struct winprint_data *) cd;
  char *queryproc;
  char *textproc;
  struct print_text_options pto;
  PRINTDLG pd;
  int cancelled;
  int top, bottom, left;
  TEXTMETRIC tm;
  POINT pt;
  int lineheight;
  int pageno;
  int error=0, done, needquery;
  struct {
	 short len; /* Defined to be 16 bits.... */
	 char buffer[PRINT_BUFSIZE+1];
  } indata;

  queryproc = argv[2];
  textproc = argv[3];
 
  if (winprint_print_text_options (wd, interp, argc, argv, &pto) != TCL_OK)
    return TCL_ERROR;

  if (winprint_print_text_dialog (wd, interp, &pto, &pd, &cancelled) != TCL_OK)
    return TCL_ERROR;
  if (cancelled)
    return TCL_OK;

  if (pto.postscript)
  {
	int eps_printing = 33;
	int result;
	short bresult = 1; /* EPS printing download suppressed */
	result = Escape (pd.hDC, eps_printing, sizeof (BOOL), (LPCSTR)&bresult, NULL);
	if ( result < 0 )
	{
		/* The EPSPRINTING escape failed! */
		Tcl_AppendElement(interp, 
                   "ide_winprint: EPSPRINTING escape implemented but failed");
		DeleteDC (pd.hDC);
		return TCL_ERROR;
	  }
  }
  else
  {
	winprint_get_margins(wd, &pd, &top, &left, &bottom);
  }

  if (winprint_start (wd, interp, &pd, &pto, &cancelled) != TCL_OK)
    {
      DeleteDC (pd.hDC);
      return TCL_ERROR;
    }
  if (cancelled)
    {
      DeleteDC (pd.hDC);
      return TCL_OK;
    }

  /* init and start init-procedure if available */
  if (pto.initproc != NULL)
  {
    	Tcl_DString initStr;
	char buf[64];
	Tcl_DStringInit (&initStr);
	Tcl_DStringAppend (&initStr, pto.initproc, -1);
	
	/* Here we must pass the customer selection from the PrintDialog
	 * as parameters for the init command, */
	/* From page */
	Tcl_DStringAppendElement (&initStr, "-frompage");
	sprintf (buf, "%i", pd.nFromPage);
	Tcl_DStringAppendElement (&initStr, buf);
	/* To Page */
	Tcl_DStringAppendElement (&initStr, "-topage");
	sprintf (buf, "%i", pd.nToPage);
	Tcl_DStringAppendElement (&initStr, buf);
	/* # Copies */
	Tcl_DStringAppendElement (&initStr, "-copies");
	sprintf (buf, "%i", pd.nCopies);
	Tcl_DStringAppendElement (&initStr, buf);
	/* Print Selection? */
	Tcl_DStringAppendElement (&initStr, "-selection");
	Tcl_DStringAppendElement (&initStr, (pd.Flags&PD_SELECTION) ? "1" : "0");
	
	/* Execute tcl/command */
	if (Tcl_Eval (interp, Tcl_DStringValue(&initStr)) != TCL_OK)
	{
	      Tcl_DStringFree (&initStr);
	      return TCL_ERROR;
	}
	Tcl_DStringFree (&initStr);
  }
    
  if (pto.postscript)
  {
    Tcl_DString pageStr;
    int status, retval, len, i;
    char *l, msgbuf[128];
    enum winprint_query q = 0;
    
    /* Note: NT 4.0 seems to leave the default CTM quite tiny! */
    strcpy (indata.buffer, "\r\nsave\r\ninitmatrix\r\n");
    indata.len = strlen(indata.buffer);
    Escape(pd.hDC, PASSTHROUGH, 0, (LPCSTR)&indata, NULL);
    
    /* Init command for page-procedure */
    if (pto.pageproc != NULL)
      {
	Tcl_DStringInit (&pageStr);
	Tcl_DStringAppend (&pageStr, pto.pageproc, -1);
	Tcl_DStringAppendElement (&pageStr, "-1");
      }
    
    /* Start printing */
    while (1)
      {
    	/* Run page-procedure to update the display */
	status = winprint_print_text_invoke (interp, Tcl_DStringValue(&pageStr), "page", &q);
	if (status != TCL_OK || q == Q_DONE)
	  {
	    error = 1;
	    break;
	  }
	
	/* query next characters to send to printer */
	if (winprint_print_text_invoke (interp, queryproc, "query", &q) != TCL_OK)
	  {
	    error = 1;
	    break;
	  }
	if (q != Q_CONTINUE)
	  {
	    done = 1;
	    break;
	  }
	if (Tcl_Eval (interp, textproc) == TCL_ERROR)
	  {
	    error = 1;
	    break;
	  }
	l = Tcl_GetStringFromObj (Tcl_GetObjResult (interp), &len);
	for (i=0; i<len; i+=PRINT_BUFSIZE)
	  {
	    int lpos = min (PRINT_BUFSIZE, len-i);
	    strncpy (indata.buffer, l+i, lpos);
	    indata.buffer[lpos] = 0;
	    indata.len = lpos;
	    
	    retval = Escape (pd.hDC, PASSTHROUGH, 0, (LPCSTR)&indata, NULL);
	    if (retval < 0)
	      {
		Tcl_AppendElement(interp, "ide_winprint: PASSTHROUGH Escape failed");
		error = 1;
		break;
	      }
	    else if (retval != indata.len)
	      {
		sprintf(msgbuf, "ide_winprint: Short write (%d vs. %d)", retval, indata.len);
		Tcl_AppendElement(interp, msgbuf);
		error = 1;
		break;
	      }
	  }
      }
    
    strcpy (indata.buffer, "\r\nrestore\r\n");
    indata.len = strlen(indata.buffer);
    Escape(pd.hDC, PASSTHROUGH, 0, (LPCSTR)&indata, NULL);
  }
  else
    {
      GetTextMetrics (pd.hDC, &tm);
      pt.x = 0;
      pt.y = tm.tmHeight + tm.tmExternalLeading;
      LPtoDP (pd.hDC, &pt, 1);
      lineheight = pt.y;
      
      pageno = 1;
      
      /* The main print loop.  */
      done = 0;
      error = 0;
      needquery = 1;
      while (1)
	{
	  int y;
	  
	  if (wd->aborted)
	    break;
	  
	  /* Start a new page.  */
	  if (pto.pageproc != NULL)
	    {
	      Tcl_DString ds;
	      char buf[20];
	      enum winprint_query q;
	      int status;
	      
	      Tcl_DStringInit (&ds);
	      Tcl_DStringAppend (&ds, pto.pageproc, -1);
	      sprintf (buf, "%d", pageno);
	      Tcl_DStringAppendElement (&ds, buf);
	      
	      status = winprint_print_text_invoke (interp, Tcl_DStringValue (&ds),
						   "page", &q);
	      
	      Tcl_DStringFree (&ds);
	      
	      if (status != TCL_OK)
		{
		  error = 1;
		  break;
		}
	      
	      if (q == Q_DONE)
		{
		  done = 1;
		  break;
		}
	    }
	  
	  if (needquery)
	    {
	      enum winprint_query q;
	      
	      if (winprint_print_text_invoke (interp, queryproc, "query", &q)
		  != TCL_OK)
		{
		  error = 1;
		  break;
		}
	      
	      if (q == Q_DONE)
		{
		  done = 1;
		  break;
		}
	      
	      /* Ignore Q_NEWPAGE, since we're about to start a new page
		 anyhow.  */
	      
	      needquery = 0;
	    }
	  
	  if (StartPage (pd.hDC) <= 0)
	    {
	      windows_error (interp, "StartPage");
	      error = 1;
	      break;
	    }
	  
	  y = top;
	  
	  /* Print a page.  */
	  
	  while (1)
	    {
	      char *l;
	      int len;
	      enum winprint_query q;
	      
	      if (Tcl_Eval (interp, textproc) == TCL_ERROR)
		{
		  error = 1;
		  break;
		}
	      
	      l = Tcl_GetStringFromObj (Tcl_GetObjResult (interp), &len);
	      
	      TextOut (pd.hDC, left, y, l, len);
	      y += lineheight;
	      
	      if (y >= bottom)
		{
		  needquery = 1;
		  break;
		}
	      
	      if (winprint_print_text_invoke (interp, queryproc, "query", &q)
		  != TCL_OK)
		{
		  error = 1;
		  break;
		}
	      
	      if (q == Q_DONE)
		{
		  done = 1;
		  break;
		}
	      else if (q == Q_NEWPAGE)
		break;
	    }
	  
	  if (error)
	    break;
	  
	  if (EndPage (pd.hDC) <= 0)
	    {
	      /* It's OK for EndPage to return an error if the print job
		 was cancelled.  */
	      if (! wd->aborted)
		{
		  windows_error (interp, "EndPage");
		  error = 1;
		}
	      break;
	    }
	  
	  if (done)
	    break;
	  
	  ++pageno;
	}
    }
  
  if (winprint_finish (wd, interp, &pd, error) != TCL_OK)
    error = 1;
  
  if (error)
    return TCL_ERROR;
  
  Tcl_ResetResult (interp);
  return TCL_OK;
}